This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Problems with transitive/cascading persistence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problems with transitive/cascading persistence" Watch "Problems with transitive/cascading persistence" New topic
Author

Problems with transitive/cascading persistence

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello,

I have an entity Poll that can contain multiple entities PossibleAnswer. They are mapped by two tables, one with a pk (Poll) the other with a pk and a fk (PossibleAnswer). It is a bidirectional relationship.


Here is the relevant snippet from PossibleAnswer:

[code] @ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="idPoll")
private Poll poll;[code]

Here is the relevant snippet from Poll

[code]@OneToMany(mappedBy="poll")
private Collection<PossibleAnswer> possibleAnswers = new ArrayList<PossibleAnswer>();[code]

I then set the Poll on each possible answer
[code]possibleAnswer1.setPoll(poll)[code]

I then add the possible Answers to the collection.
[code]possibleAnswers.add(possibleAnswer1)[code]

I then set the collection to the Poll as follows:
[code]poll.setPossibleAnswers(possibleAnswers)[code]

and finally I persist the poll.

Only the poll entity gets persisted and therefore the transitive persistence does not work.

I use hibernate as jpa provider.

Can anyone please help?

Julien.
[ November 19, 2007: Message edited by: Julien Martin ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yes you only set the cascade option for one of the directions, not the other. So set the OneToMany mapping to have a cascade option to save it.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
thanks Mark!
 
Consider Paul's rocket mass heater.
 
subject: Problems with transitive/cascading persistence
 
Similar Threads
Cast Entity association collection to implementation type
JBoss CMP read ahead cache problem
CMP beans not entering data in database
Mysql auto generated PK used for another table and JPA
Container Error in FindBy methods in JBoss