-->
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.  [ 6 posts ] 
Author Message
 Post subject: Cascade Creation
PostPosted: Wed Aug 12, 2009 11:40 am 
Newbie

Joined: Sat Mar 29, 2008 4:49 pm
Posts: 19
I have an application which creates a complex structure of objects that are meant to be persisted. Currently, in order to persist these objects, I manually save each object to my Hibernate session starting with the lowermost (least dependent) object working my way upward. Is there a way to cascade the creation of objects in a complex structure such that all I need to do is save the highest object in the structure (most dependent) and Hibernate automatically creates the persisted objects in the structure?


Top
 Profile  
 
 Post subject: Re: Cascade Creation
PostPosted: Wed Aug 12, 2009 11:58 am 
Newbie

Joined: Sat Mar 29, 2008 4:49 pm
Posts: 19
I should probably have added that I've map entities which use many-to-many associations in both the index as well as value components. While cascade seems to work for values, it seems to ignore the indices (see my other post on deleting indices in maps).


Top
 Profile  
 
 Post subject: Re: Cascade Creation
PostPosted: Wed Aug 12, 2009 12:03 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Have you tried with cascade annotations?
Your question is very generic, so my answer must to be too. We also have a complex structure and we use Cascade annotation (org.hibernate.annotations.Cascade). it works fine for _ToMany associations (even @ManyToMany).

What do you think?

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: Cascade Creation
PostPosted: Wed Aug 12, 2009 10:53 pm 
Newbie

Joined: Sat Mar 29, 2008 4:49 pm
Posts: 19
Yes, I must admit the way my query was posted was pretty generic. At this point, it's just hard to decide what detail to put down.

And no, I haven't tried configuring "cascade" via annotations. I just assumed that xml config being the older, it would be more mature. I'll give it a try, but I can't figure out why the former wouldn't work.

For additional info, here's what I have:
Code:
class A {
  long id;
  String name;
}

class B {
  long id;
  String name;
}

class C {
  long id;
  Map<B, A> transition;
}

class D {
  long id;
  Map<A, C> transitionTable;
}

And XML mapping:
Code:
<class name="C" table="C">
  <id name="id">
    <generator class="sequence" />
  </id>
  <map name="transitions" table="B_TO_A" cascade="all,delete-orphan">
    <key column="TRANSITIONS_ID" />
    <index-many-to-many class="B" column="B_ID" />
    <many-to-many column="A_ID" class="A" />
  </map>
</class>

<class name="D" table="D">
  <id name="id">
    <generator class="sequence" />
  </id>
  <map name="transitionTable" table="A_TO_C"
    cascade="all,delete-orphan">
    <key column="TRANSITION_TABLE_ID" />
    <index-many-to-many class="A" column="A_ID">
    </index-many-to-many>
    <many-to-many class="C" column="C_ID" />
  </map>
</class>


Top
 Profile  
 
 Post subject: Re: Cascade Creation
PostPosted: Thu Aug 13, 2009 1:27 am 
Newbie

Joined: Sat Mar 29, 2008 4:49 pm
Posts: 19
One more observation: it seems that the cascade effect doesn't work on objects which are collection indices. I've verified that objects as values get the cascading effect (whether its persisting or deleting) but objects reference as indices (map index, in this case), don't.

Is this a known issue? Will switching to annotations fix this?


Top
 Profile  
 
 Post subject: Re: Cascade Creation
PostPosted: Thu Aug 13, 2009 12:28 pm 
Newbie

Joined: Sat Mar 29, 2008 4:49 pm
Posts: 19
So I switched to annotations and it gave me the exact same problem (no Cascading on the index). Here's my annotated class:
Code:
public class C {
   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   long id;

   @ManyToMany(cascade=CascadeType.ALL)
   @JoinTable(name="B_TO_A", inverseJoinColumns=@JoinColumn(name="A_ID"))
   @MapKeyManyToMany(targetEntity = B.class, joinColumns=@JoinColumn(name="B_ID"))
   Map<B, A> transition;
}

I would appreciate any help / insight.


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