The below code seem simple, yet it takes me long time but turned out to be cumbersome and lengthy code even i dislike. could someone help me with some efficient code? many thanks. by the way, i'm using hibernate 3.6 JPA implementation
@Entity
class X
{
@OneToMany( fetch = FetchType.EAGER, mappedBy = "x", cascade = { CascadeType.PERSIST, CascadeType.MERGE } )
private Set<Y> ys = new HashSet<Y>();
public void persist()
{
//here, this(x) is newly create but its ys are already in the DB, so how to write the code?
}
public void merge()
{
//like persist(), the ys of this(x) is changed, how to merge effiently?
}
}
i use the below but it will throw exception: Cannot fetch unpersisted entity
public void merge()
{
EntityManager em = entityManager();
EntityTransaction tx = em.getTransaction();
try
{
tx.begin();
for(Y y: ys)
em.merge(y);
em.merge(this);
tx.end();
}
finally
{
...
}
}