Greetings,
One simple question, lets say I have classes:
[A] 1-----*
[B]and i have it annotated this way:
Code:
@Entity
class A implements Serializable
{
@OneToMany(fetch=FetchType.EAGER)
@JoinTable(name="JOIN_TABLE", joinColumns=@JoinColumn(name="A_ID"), inverseJoinColumns=@JoinColumn(name="B_ID"))
public Set<B> getBs() {
....
}
}
@Entity
class B implements Serializable
{
}
And I wish to update class A, but I don't want to perform any modification in class B when updating it through the class A DAO, (please note that I'm using spring 3 and transactional management at implementing services) that it goes like:
Code:
@Service("aService")
public class A_ServiceImpl implements A_Service {
@Autowired
private A_DAO aDao;
@Transactional(propagation= Propagation.REQUIRED, readOnly=false)
public void updateA(A a)
{
aDao.update(a);
}
}
@Repository("aDao")
public class A_DAOImpl implements A_DAO {
@Autowired
private SessionFactory sessionFactory;
public void update(A a)
{
Session session = sessionFactory.getCurrentSession();
session.update(user);
}
}
If I go ahead and execute the update from X place, I get:
Quote:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing
This can be corrected changing the association annotation to perform cascading, like:
Code:
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinTable(name="JOIN_TABLE", joinColumns=@JoinColumn(name="A_ID"), inverseJoinColumns=@JoinColumn(name="B_ID"))
public Set<B> getBs() {
....
}
and this is all right if I wished to perform a modification of both classes, but in my case, I want the update operation to ignore class B and leave class B's table intact, I have tried including
insertable=false, updatable=false, but it stills tries to perform an update in class B.
The root cause that I want the immutability at the update is because class B also has a many-to-one to class C, and class C has a another many-to-one to class D, and if I specify cascading (SAVE_UPDATE / ALL), when performing the update of class A, a duplication of class D (or insertion of a new record containing the same data) will be saved in the database with a new ID and causing constraint violation on unique columns; I also tried adding
insertable=false, updatable=false to the many-to-one relation in class B and C but this seems to be ignored some how, this is how I annotated class B, and class C
Code:
@Entity
class B implements Serializable
{
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="JOIN_TABLE_BC")
public C getC() {
.....
}
class C implements Serializable
{
@ManyToOne
@JoinColumn(name="JOIN_TABLE_CD", insertable=false, updatable=false)
public D getD() {
.....
}
please help, I presently don't have a generous person to place a pair of fresh eyes
regards