-->
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.  [ 5 posts ] 
Author Message
 Post subject: OneToOne foreign keys
PostPosted: Mon Jun 18, 2007 7:19 am 
Newbie

Joined: Sat Jun 16, 2007 2:12 pm
Posts: 2
Hi, I'm new to Hibernate and having problems getting a foreign key mapping working.

I have the following simple table structure which maps users to preferences:

Code:
CREATE TABLE Users
(
   userId    int   IDENTITY NOT NULL,
   userName varchar(50) NOT NULL
)

CREATE TABLE UserPreferences
(
   prefId   int   IDENTITY NOT NULL ,
   userId   int   NOT NULL ,
   preferenceString   text NOT NULL
)


My class implementations are as below (based on the Hibernate Owner/OwnerAddress example):

Code:

@Entity
@Table (name="Users")
public class User
{
   @Id
   @GeneratedValue (strategy=GenerationType.IDENTITY)
   @Column (name="userId")
   private Integer   userId;
   
   @Column (name="userName")
   private String   userName;
   
   @OneToOne (cascade=CascadeType.ALL)
   @JoinColumn (name="userId")
   private Preferences   preferences;
   
   //   get/set methods...
}

@Entity
@Table (name="UserPreferences")
public class Preferences
{
   @Id
   @GeneratedValue (strategy=GenerationType.IDENTITY)
   @Column (name="prefId")
   private Integer   prefId;
   
   @GeneratedValue(generator = "fk")
      @GenericGenerator (strategy = "foreign", name = "fk",
         parameters = @Parameter(name="property", value="user"))
   @Column (name="userId")
   private Integer   userId;
   
   @OneToOne (mappedBy="preferences", optional=false)
   private User user;
   
   @Column (name="preferenceString")
   private String preferenceString;
   
   //   get/set methods...
}


and finally my test case is as follows:

Code:
@Test
public void testSaveUserAndPreference() throws Exception
{
   Session   session = HibernateUtil.getSessionFactory().openSession();
   Transaction   tx = session.beginTransaction();

   User      user = new User();
   Preferences   prefs = new Preferences();

   prefs.setPreferenceString("test preferences");
   prefs.setUser(user);
   user.setPreferences(prefs);
   user.setUserName("test user with prefs");

   session.persist(user);
   tx.commit();
   session.close();

   HibernateUtil.shutdown();      
}


The problem I am having is that the userId is not being set on the UserPreference and hence gives me an insert exception:

Code:
Hibernate:
    /* insert core.User
        */ insert
        into
            Users
            (userName)
        values
            (?)
Hibernate:
    /* insert core.Preferences
        */ insert
        into
            UserPreferences
            (preferenceString, userId)
        values
            (?, ?)
12:08:22,265  WARN JDBCExceptionReporter:77 - SQL Error: 515, SQLState: 23000
12:08:22,265 ERROR JDBCExceptionReporter:78 - Cannot insert the value NULL into column 'userId', table 'dbo.UserPreferences'; column does not allow nulls. INSERT fails.


Any ideas what I am doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 11:01 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Have a look at the "foreign" generator

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 11:47 am 
Newbie

Joined: Sat Jun 16, 2007 2:12 pm
Posts: 2
thanks for the reply. i thought i had done this with this:

Code:
   @GeneratedValue(generator = "fk")
      @GenericGenerator (strategy = "foreign", name = "fk",
         parameters = @Parameter(name="property", value="user"))
   @Column (name="userId")
   private Integer   userId;
   
   @OneToOne (mappedBy="preferences", optional=false)
   private User user;


am i doing something wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 6:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you need to put the generators on the
private Integer userId;
property along with @Id

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Did it work with the change suggested?
PostPosted: Tue Sep 25, 2007 11:55 am 
Newbie

Joined: Tue Sep 25, 2007 11:37 am
Posts: 2
@Id
@GeneratedValue(generator = "fk")
@GenericGenerator (strategy = "foreign", name = "fk",
parameters = @Parameter(name="property", value="user"))
@Column (name="userId")
private Integer userId;

@OneToOne (mappedBy="preferences", optional=false)
private User user;

For some reason it does not seem to work. I thought this is what Emmanuel suggested

- Ravinder.


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