Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problems retrieving a joined entity object(Calling join methods on a recently created object doesn't work)
Problems retrieving a joined entity object [message #778767] Fri, 13 January 2012 19:46 Go to next message
John Dugo is currently offline John DugoFriend
Messages: 13
Registered: October 2011
Junior Member
I am seeing an issue retrieving joined objects on a recently created (and merged) entity object.

Here is the scenario:

class A {
@Column("A_ID")
Long getObjectId();
void setObjectId(Long id);
}

class B {
@Column("B_ID")
Long getObjectId();
void setObjectId(Long id);
@Column("A_ID")
Long getAId();
void setAId(Long aId);
@JoinColumn(name="AID")
@ManyToOne(fetch=Lazy)
A getA();
void setA(A a);
}


The flow is as follows:

Object A is created and merged.
A a = new A();
a.setObjectId(1);
entityManager.merge(a);

Object B is created, the foreign key id for A is set, and merged:
B b = new B();
b.setObjectId(2);
b.setAId(1);
entityManager.merge(b);

Object B is loaded.

B b = m_entityManager.find(B.class,2)

A a = b.getA();
//a == null!

When I try to retrieve the joined A object off of B, it is always null. Now if I wait long enough and the cache is refreshed, calling b.getA() will correctly return the object.

All of these calls are happening in consecutive calls to the server, so the transactions are committed and the data is in the database. My cache is currently set to an isolation level of Shared using a cache type of Weak.

Note that I am never explicitly setting b.setA(a) before I call merge, but I dont feel like I should have to. I am setting the foreign key id and I have the join set to be lazily loaded.

Is there something that I am doing wrong or a setting that I am missing? I dont want to have to separately load the object, I should just be able to get it from the join.
Re: Problems retrieving a joined entity object [message #779768 is a reply to message #778767] Mon, 16 January 2012 14:17 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Hello,

Yes, you have to call b.setA(a) for the relationship to be maintained. JPA allows for caching to maintain object identity, which needs to be kept in synch with what is in the database or you must force a refresh to see the data.

The code you posted though is incorrect for what you mention you want. B's aId field uses database field "A_ID" while its a attribute uses "AID" as the foreign key. They are different, so calling setAId(1) will not change the foreign key in the database. If this was not a post cut and paste error, both mappings should use the same field, and one of them should be marked as insertable=false, updatable=false, so that the JPA provider can tell which of them controls the field or you will get an exception. You will still need to set the relationship (and/or the basic mapping) or refresh the object when the transaction completes to see the object as it is in the database.

Best Regards,
Chris
Re: Problems retrieving a joined entity object [message #779770 is a reply to message #778767] Mon, 16 January 2012 14:17 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Hello,

Yes, you have to call b.setA(a) for the relationship to be maintained. JPA allows for caching to maintain object identity, which needs to be kept in synch with what is in the database or you must force a refresh to see the data.

The code you posted though is incorrect for what you mention you want. B's aId field uses database field "A_ID" while its a attribute uses "AID" as the foreign key. They are different, so calling setAId(1) will not change the foreign key in the database. If this was not a post cut and paste error, both mappings should use the same field, and one of them should be marked as insertable=false, updatable=false, so that the JPA provider can tell which of them controls the field or you will get an exception. You will still need to set the relationship (and/or the basic mapping) or refresh the object when the transaction completes to see the object as it is in the database.

Best Regards,
Chris
Previous Topic:DBWS: java.sql.SQLSyntaxErrorException
Next Topic:Extension Attributes- @VirtualAccessMethods
Goto Forum:
  


Current Time: Fri Dec 19 04:05:46 GMT 2014

Powered by FUDForum. Page generated in 0.01621 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software