-->
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.  [ 1 post ] 
Author Message
 Post subject: problem in Many-toOne RelationShip
PostPosted: Thu Feb 16, 2012 10:33 am 
Newbie

Joined: Thu Feb 16, 2012 10:30 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.