Bug 184 : Doesn't work cascade persisting using auto-increment
Priority 
High
Reported Version 
 
Logged By 
Yaroslav Kosinov
Status 
Fixed
Fixed Version 
2.3.0
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
30/11/2009
Updated 
30/11/2009
Type 
Bug
 
Attachments 
No attachments

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.

 
Rob 02 Dec 06:50
Hmmm - couldn't reproduce.

Hmmmm, seemed to work for me - aka the person_id's where inserted with values.

trans[1003], 06:42:33.818, Delete table[PHONES] rows[0] bind[null]
trans[1003], 06:42:33.823, Delete table[PERSONS] rows[0] bind[null]
trans[1004], 06:42:33.935, insert into PERSONS (ID, SURNAME, NAME) values (?, ?, ?)
trans[1004], 06:42:33.936, Binding Insert [PERSONS] set[id=1, surname=Kosinov, name=Yaroslav, ]
trans[1004], 06:42:34.436, Inserted [Person] [1]
trans[1004], 06:42:34.443, insert into PHONES (ID, PHONE_NUMBER, PERSON_ID) values (?, ?, ?)
trans[1004], 06:42:34.444, Binding Insert [PHONES] set[id=1, phoneNumber=5244011, PERSON_ID=1, ]
trans[1004], 06:42:34.446, Inserted [Phone] [1]
trans[1005], 06:42:34.466, insert into PERSONS (ID, SURNAME, NAME) values (?, ?, ?)
trans[1005], 06:42:34.467, Binding Insert [PERSONS] set[id=2, surname=Kosinov, name=Gennady, ]
trans[1005], 06:42:34.468, Inserted [Person] [2]
trans[1005], 06:42:34.468, insert into PHONES (ID, PHONE_NUMBER, PERSON_ID) values (?, ?, ?)
trans[1005], 06:42:34.468, Binding Insert [PHONES] set[id=2, phoneNumber=5712658, PERSON_ID=2, ]
trans[1005], 06:42:34.469, Inserted [Phone] [2]

Rob 02 Dec 06:55
Transaction logs... or Asserts ?

Can you provide the transaction log output? ... or perhaps some Assert statements that fail - showing the problem?

Thanks, Rob.

Rob 06 Dec 08:58
Opps DB2

As an update ... this is DB2, and currently looking into platform support for DB2.

Rob 15 Dec 14:20
Hmmm...

Sent a DBPlatform and waiting to see how that went.

Rob 17 Dec 10:38
I'll close this...

I'll close this... can re-open if anyone gets interested in following through with DB2.

If you want DB2 support please let me know.

woResponse

Upload a file