Lazy fetched ManyToOne field of an embedded object of a newly persisted entity resets to null [message #547594] |
Mon, 19 July 2010 06:32 |
Igor Mukhin Messages: 14 Registered: July 2009 |
Junior Member |
|
|
Hello everybody
(EclipseLink 2.1 with static weaving)
These are my entities:
Simple entity with id and an embedded object:
@Entity
public class Entity1 implements Serializable {
@Id private int id;
@Embedded private Embedded1 embedded1;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public Embedded1 getEmbedded1(){ return embedded1; }
public void setEmbedded1(Embedded1 embedded1) { this.embedded1 = embedded1; }
}
A embaddable with one ManyToOne with FetchType.LAZY field.
@Embeddable
public class Embedded1 implements Serializable {
@ManyToOne(fetch=FetchType.LAZY)
private Person person;
public Embedded1() { }
public Embedded1(Person person) { this.person = person; }
public Person getPerson() { return person; }
public void setPerson(Person person) { this.person = person; }
}
And a Person entity
@Entity
public class Person implements Serializable {
@Id private int id;
private String name;
... getters / setters for id and name
}
Then the code
@Component("testim")
@Scope("request")
public class TestIMBean {
@Autowired EntityManager em;
private int entity1id;
public String createEntity1() {
Person person = em.find(Person.class, 1);
Entity1 e1 = new Entity1();
e1.setId(entity1id);
e1.setEmbedded1(new Embedded1(person));
em.getTransaction().begin();
em.persist(e1);
em.getTransaction().commit();
entity1id = e1.getId();
return null;
}
public String readEntity1() {
Entity1 e1 = em.find(Entity1.class, entity1id);
Person person = e1.getEmbedded1().getPerson();
if (person == null) {
Faces.error("e1.getEmbedded1().getPerson() is null");
return null;
}
System.out.println(person.getFirstname());
Faces.info("Read. Person is " + e1.getEmbedded1().getPerson().getFirstname());
return null;
}
}
Then i call createEntity1() which creates an entity with an id from entity1id field. That works fine. (The transaction gets commit, which is important step)
Then i use call readEntity1() to read the entity. This entity will be read from EclipseLink cache fine, BUT wenn I call e1.getEmbedded1().getPerson() it returns null.
I found out that
1) if I clear the cache emf.getCache().evictAll(), the entity will be reread from the database and the problem field is fine.
2) this happen only to the newly persisted objects
3) if you remove FetchType.LAZY, it also works fine
4) if you move person field from Embedded1 directoctly to Entity1, it also works fine
So the removal of FetchType.LAZY in embedded object is the best workaround for us. But it is not the best for the performance.
Is it a bug or do I do something wrong?
Thanks
[Updated on: Mon, 19 July 2010 09:17] Report message to a moderator
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02704 seconds