-->
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.  [ 4 posts ] 
Author Message
 Post subject: Transient object Exception
PostPosted: Mon Sep 29, 2008 6:36 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I'm still learning the do's and don'ts about Hibernate and I had a question. It has to do with persisting collections and cascading. Here's my pseudo-code:

Code:
Main Entity Class:
...
   @OneToMany (
      mappedBy = "mainEntity",
      targetEntity = Things.class,
      fetch = FetchType.EAGER,
      cascade = CascadeType.ALL
   )
   private List<Things> things= new ArrayList<Things>();
...

Collection Entity Class:
...
@ManyToOne @JoinColumn(name = "main_entity_id")
   private MainEntity mainEntity;

...


Now, when I go to update values in the things collection inside the MainEntity class, nothing happens. My code to add values to the List is this:
Code:
mainEntity.getThings().add(value);
mainEntity.getThings().addAll(values);


When I do this, nothing happens as far as Hibernate is concerned. Yes, the Things objects are added to the transient MainEntity class, but not to the database in any way. So, I have to call this:
Code:
mainEntity.getThings().addAll(values);
List<Things> thingList = new ArrayList<Things>();
for (Things thing : mainEntity.getThings()) {
    thing.setMainEntity(mainEntity);
    thingList.add(thing);
}
mainEntity.setThings(thingList);

myDao.update(mainEntity);

whenever I make an update to the collection values. Kind of a bit of a workaround, wouldn't you say?

Now, if I omit this code and just call update after adding values to the mainEntity.getThings() List, I get a Transient Object Exception (HibernateException) saying that the added Things don't belong to the mainEntity instance or something like that.

To prevent redundancy and wasting of time, is there a way I can do this without having to define all of this for every collection I persist?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 10:55 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
rfkrocktk ,

Whenever you use a child collection from a parent object, bear in mind that you are using a "leaky abstraction". This means that Hibernate will keep the fiction that "whatever you add to the child collection will end up in the database", but only only to a certain extent.
As soon as you start creating separate lists and using addAll(), you basically "lost" Hibernate, it stops working for you.

In your example, you are defining a bidirectional association. In those cases, one of then does the real SQL work, and the other has to be marked "inverse". How is that reflected in your code?

Also, consider that the proper way to assign a child, in your parent class, is to create your own custom method, which not only adds the child to the list, but sets itself as the parent of this new child.

Code:
public void addThing(Thing obj){
  this.thingsList.add(obj);
  obj.setParent(this);
}



Of course, you can easily modify this to add several children at a time.
But, do you understand why a simple addAll() won't do?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2008 1:05 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
Gonzalo,
Thanks, it totally makes sense.

I've actually been attempting to create a unidirectional association between two entities, but I've been totally unsuccessful in doing that up to this point. Without including the reference in the owned entity, I have been unable to map a collection using a join column or join table.

I'll definitely implement what you've suggested into my code, thanks so much for your help.
- TK


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2008 6:24 pm 
Newbie

Joined: Fri Nov 21, 2008 3:35 pm
Posts: 12
If you haven't already, view the Hibernate documentation online. It's worth reading through to understand the ins and outs of Hibernate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.