-->
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: Simple question: How to persist child with only parentId?
PostPosted: Wed Jul 27, 2011 8:02 am 
Newbie

Joined: Wed Jul 27, 2011 7:50 am
Posts: 2
I have two classes:
Code:
Parent:
@Entity
public class Leilighet {
   
   @Id @GeneratedValue(strategy = GenerationType.AUTO)
   private Long leilighetId;
   private String adresse;
   private String postNr;
   private String postSted;
   
   @OneToMany
   @JoinColumn (name = "leilighetId")
   @LazyCollection (LazyCollectionOption.FALSE)
   private List<Rom> rom;
...
}

Child:
@Entity
public class Rom implements Comparable<Rom>, Serializable {

   private static final long serialVersionUID = -5042142853064435303L;
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long romId;
   private String romNummer;
   private String beskrivelse;

   @ManyToOne
   @JoinColumn(name = "leilighetId", insertable = false, updatable = false, nullable = false)
   private Leilighet leilighet;
...
}


I have lots of parents (Leiligheter) in the database already. I've created a form to add Children (Rom).
The problem is that when i try to persist the children, the reference to the parent is lost. Thus creating a constraint violation.

In my action class i have tried two ways of setting the reference to the parent:
Code:
Version 1:
         rom = new Rom();
         if (leilighetId != null) {
            Leilighet leilighet = leilighetManager.get(leilighetId);
            rom.setLeilighet(leilighet);
         }
Version2:
         rom = new Rom();
         if (leilighetId != null) {
            Leilighet leilighet = new Leilighet();
            leilighet.setLeilighetId(leilighetId);
            rom.setLeilighet(leilighet);
         }

None of them works for me.

I guess this should be pretty simple once I figure out what im doing wrong. How can a make hibernate persist the child with the reference to the parent?

Thanks guys!


Top
 Profile  
 
 Post subject: Re: Simple question: How to persist child with only parentId?
PostPosted: Wed Jul 27, 2011 9:45 am 
Newbie

Joined: Wed Jul 27, 2011 7:50 am
Posts: 2
Never mind. Found the solution myself. It was the "insertable=false, updateble=false" that made it fail. Removed it and it works like a charm.


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.