Hello,
I don’t understand the behavior of Hibernate in Many-to-One relationships, when the foreign key is also part of primary key.
I have, for example, these classes:
@Entity public class Country implements Serializable { @Id private String name; … }
and
@Entity @IdClass(value=StateId.class) public class State implements Serializable { @Id private String name; @Id @ManyToOne private Country country; … }
Every time you try to make persistent a new object of State class, also hibernate tries to insert a new Country record in the system. The problem is that relationship is Many-to-One. Often it's probably, the country exists in database and therefore a constrain violation primary key is thrown. I think if you make persistent a new State object, the value of the Country that is part of the primary key should be previously persistent and therefore Hibernate shouldn’t attempt to insert a new country record.
Is there any way change the default behavior of Hibernate?
Thanks in advanced Josep
|