-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapkey cascade?
PostPosted: Fri Aug 20, 2010 4:30 pm 
Newbie

Joined: Mon Aug 02, 2010 10:56 am
Posts: 6
I have a problem with cascade and maps.
The classes are:

Campaign.java
Code:
...   
@OneToMany(cascade=CascadeType.ALL)
@MapKey(name="position")
private Map<Integer,Ad> ads;
...


Ad.java
Code:
...
@ManyToOne(optional=false)
private Campaign campaign;
...


When I try to save a Campaign object with some Ads, it throws exception for not null field "campaign_id" in the ads table.
If I take optional=false it works but no reference is kept for the Ad parent (campaign_id = NULL).
I can't set the id for the parent because it doesn't exists yet.

Am I missing something?


Top
 Profile  
 
 Post subject: Re: Mapkey cascade?
PostPosted: Fri Aug 20, 2010 4:34 pm 
Newbie

Joined: Mon Aug 02, 2010 10:56 am
Posts: 6
I found the solution myself almost instantly.
You have to reference the Campaign in the Ad like:
Code:
Ad.setCampaign(campaign);

Even though campaign was not saved yet and there's no cascade from Ad to Cascade.

The catch is that you have to do both, add the Ad to the Ads map of the Campaign and set the campaign in the Ad (quite confusing that the object is called Ad haha)

Hope it works for someone else!


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