-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: [Hibernate Annotation] How to ignore a child/relation update
PostPosted: Tue Feb 22, 2011 7:00 pm 
Newbie

Joined: Tue Mar 09, 2010 11:02 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.