Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » merging returns old value for referenced entity
merging returns old value for referenced entity [message #902871] Mon, 20 August 2012 19:46 Go to next message
Amit B is currently offline Amit BFriend
Messages: 12
Registered: July 2012
Junior Member
public class B{
@Id
@Column(name = "id", nullable = false)
private Integer id;
@Version
private int version;
@Column(name = "name", nullable = false)
private String name;

@Override
public SerializableEntity copy() {
return new B(this);
}

public B() {
}

public B(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

public class A {
@Id
@Column(name = "id", nullable = false)
private Integer id;
@Version
private int version;
@Transient
private Integer B_id = 0;

@ManyToOne(targetEntity = B.class)
@JoinColumn(name = "B_ID", referencedColumnName = "id")
private B b_type;

public B getBType() {
if (b_type == null && B_id != 0) {
b_type = readSingleEntityById(B.class, B_id);
}
return b_type;
}

public void setBType(B ent) {
b_type = ent;
if (b_type != null)
B_id = b_type.getId();
else
B_id = getBTypeId();
}

public Integer getBTypeId() {
if (B_id == 0 && getBType() != null)
B_id = b_type.getId();
return B_id;
}

@Override
public SerializableEntity copy() {
return new A(this);
}

public A() {
}

public Integer getId() {
if (id == null) {
return 0;
} else {
return id;
}
}

public void setId(Integer id) {
this.id = id;
}

}


public void saveA(){
try {
UserTransaction u = getUserTransaction();
u.begin();
u.merge(A);
u.commit();
} catch {
code here to catch exception
}
}

public <T> T merge(T ent) {
ent = m_em.merge(ent); ----> Problem
m_em.flush();
addMergedEntityToMyCache(ent);
return ent;
}

I have two entities A and B defined as above. A has a many-to-one relationship to Entity B.
Old Values -
b_id = 2
b_type = obj2_type.

New Values to merge. When I enter the merge method these are correct values for ent object
b_id = 4
b_type = obj4_type.

After the call to m_em.merge() the values in the Entity A are -
b_id = 2
b_type = obj4_type

The database is updated with the correct values. Disabling shared cache does not fix the problem. What am I doing wrong here ? Is the b_id as Transient an issue ?. If yes why ? I shouldn't be getting back stale id but the correct merged entity object.

Re: merging returns old value for referenced entity [message #903283 is a reply to message #902871] Wed, 22 August 2012 20:21 Go to previous messageGo to next message
Amit B is currently offline Amit BFriend
Messages: 12
Registered: July 2012
Junior Member
Just to clarify above - The behavior I see is that my transient id that is id of the referenced entity in Class A seem to retain old value while the actual referenced entity gets updated after merge. How do I keep the transient id and object in-sync. Any ideas ?
Re: merging returns old value for referenced entity [message #903593 is a reply to message #903283] Fri, 24 August 2012 13:08 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Yes, Transient is the problem. Because B_ID is transient, it is not merged or persisted to the database in anyway: it is as if it doesn't exist to JPA.

What is the point of keeping b_id in the entity when you are keeping a reference to B itself? Since the field exists, you might want to remove the @Transient and map it to the "B_ID" field but make it insertable=false, updateable=false so that the ManyToOne still controls the field. Or you can look into post merge events which can be used to fix transient fields after the merge.

Best Regards,
Chris
Re: merging returns old value for referenced entity [message #904008 is a reply to message #903593] Mon, 27 August 2012 14:53 Go to previous message
Amit B is currently offline Amit BFriend
Messages: 12
Registered: July 2012
Junior Member
Thanks Chris. I am going to add postMerge event and do some testing. We are keeping the b_id so that we can serialize/deserialize it and send it to the Client instead of the entire object. Is this a good way to go about it or you have a better suggestion ?

Thanks
Previous Topic:View as JoinTable causes delete problem
Next Topic:Thread hang in ConcurrencyManager.acquire()
Goto Forum:
  


Current Time: Fri Dec 19 15:20:49 GMT 2014

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

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