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!