We have two tables with one reference and two beans for it (Ebean version 2.2.0; RDBMS DB2 9.7.0.441).
@Entity(name = "Person")
@Table(name = "PERSONS")
public class Person implements Serializable {
private Long id;
private String surname;
private String name;
private List phones;
public Person() {
}
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "SURNAME", nullable = false, unique = false, columnDefinition = "varchar(64)")
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Column(name = "NAME", nullable = false, unique = false, columnDefinition = "varchar(64)")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(targetEntity = Phone.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")
public List getPhones() {
return phones;
}
public void setPhones(List phones) {
this.phones = phones;
}
}
@Entity(name = "Phone")
@Table(name = "PHONES")
public class Phone implements Serializable {
private Long id;
private String phoneNumber;
private Person person;
public Phone() {
}
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "PHONE_NUMBER", nullable = false, unique = true, columnDefinition = "varchar(7)")
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@NotNull
@ManyToOne(targetEntity = Person.class, cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "PERSON_ID", nullable = false)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
We want to make cascade persisting of objects in these tables in such a way:
public class DMLTest {
public DMLTest() {
}
public static void main(String[] args) {
DMLTest dmlTest = new DMLTest();
dmlTest.deleteAll();
dmlTest.insertData();
}
public void deleteAll() {
Ebean.beginTransaction();
try {
Ebean.createUpdate(Phone.class, "delete from phone").execute();
Ebean.createUpdate(Person.class, "delete from person").execute();
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
}
}
private void insertData(){
Person person=new Person();
person.setSurname("Kosinov");
person.setName("Yaroslav");
Phone phone=new Phone();
phone.setPhoneNumber("5244011");
List phones = new ArrayList();
phones.add(phone);
person.setPhones(phones);
Ebean.save(person);
person=new Person();
person.setSurname("Kosinov");
person.setName("Gennady");
phone=new Phone();
phone.setPhoneNumber("5712658");
phone.setPerson(person);
Ebean.save(phone);
}
}
Rows in the database was created without EBean exceptions BUT! all rows in the PHONES table haven't values in the PERSON_ID column :( All auto-generated values for primary keyes was created correctly.
Example of the constructed data:
PHONES
ID-PHONE-PERSON_ID
27-5244011-NULL
28-5712658-NULL
PERSONS
27-Kosinov-Yaroslav
28-Kosinov-Gennady
We think that the problem is that after Ebean.save(...) method getId() - is still equals to NULL but the value in the database already is created.