impetus-opensource/Kundera — Issue #33: loss of state after persistence in case of association handling, however persisted and find method works correctly
Author: bthor
Created: 2012-02-15 17:29:52+00:00
State: open

Kundera 2.0.5 with Mongodb.

Every time I call persist on an entity it sets the contained objects to null. The data is stored correctly in mongo and the objects can be fully reloaded as a workaround.

Here is a simple example, notice the address field is set to null when
we persist.

```
pre-persist: Person{personId='3c553dce-8a81-4137-b67c-7c49ed6e9bc6',
address=Address{addressId='325cc009-4551-4211-8962-c9b01984f457',
street='123 Kundera St.'}}

post-persist: Person{personId='3c553dce-8a81-4137-b67c-7c49ed6e9bc6',
address=null}

public class Test {

public static void main(String[] args) {

EntityManagerFactory emf =Persistence.createEntityManagerFactory("testMongo");
EntityManager em = emf.createEntityManager();

Address address = new Address();
address.setAddressId(UUID.randomUUID().toString());
address.setStreet("123 Kundera St.");

String id = UUID.randomUUID().toString();
Person person = new Person();
person.setPersonId(id);
person.setAddress(address);

System.out.println(" pre-persist: " + person.toString());
em.persist(person);
System.out.println("\n post-persist: " + person.toString());

em.close();
emf.close();
}

}

@Entity
@Table( schema = "test@testMongo", name="Person")
public class Person {

@Id
@Column(name="personId")
private String personId;

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name="addressId")
private Address address;

public String getPersonId() {
return personId;
}

public void setPersonId(String personId) {
this.personId = personId;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "Person{" +
"personId='" + personId + '\'' +
", address=" + address +
'}';
}

}

@Entity
@Table( schema = "test@testMongo", name="Address")
public class Address {

@Id
@Column(name="addressId")
private String addressId;

@Column(name = "street")
private String street;

@OneToMany(mappedBy= "address", fetch = FetchType.LAZY)
private List persons;

public String getAddressId() {
return addressId;
}

public void setAddressId(String addressId) {
this.addressId = addressId;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

@Override
public String toString() {
return "Address{" +
"addressId='" + addressId + '\'' +
", street='" + street + '\'' +
'}';
}
}

```