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.  [ 3 posts ] 
Author Message
 Post subject: Making transient instances persistent by association
PostPosted: Sat Mar 29, 2008 5:04 pm 
Newbie

Joined: Sat Mar 29, 2008 4:49 pm
Posts: 19
Hi,

Is it possible to make transient instances persistent whenever they are associated with persistent objects? In other words, if I had

Code:
Person parent = new Person();
session.save(parent);

Person child = new Person(); // child is transient
parent.addChild(child); // child becomes persistent


outside of modifying the mutator semantics and putting in Hibernate code there. If so, how is this done? Ideally, I would like the object to become persistent first before the mutator method gets called because of how I've implemented hashCode() (includes the generated identifier).

Does Hibernate wrap mutator methods?

In case I do have to change the mutator methods, is there a function that indicates whether a given object is transient or persistent? (or if it has even been defined as a Hibernate persistent class?) A'la
Code:
void addChild(Person child) {
    if (session.isTransient(child)) {
        session.persist(child);
    }
    this.child = child;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 10:39 am 
Newbie

Joined: Sun Mar 02, 2008 3:43 pm
Posts: 9
I believe you have to do it yourself but I'm not an expert.

For the second part I guess Session.contains(entity) should indicate if the entity is already persisted or not.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 4:17 pm 
Newbie

Joined: Sat Mar 29, 2008 4:49 pm
Posts: 19
Any tips on how I can do this transparently? Would anyone suggest, say, aspect programming?

Like I said, I'd rather not mix Hibernate functionality in the business logic if I can avoid it.


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

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.