-->
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: One-to-one mapping using foreign key with unique constraint
PostPosted: Sat Apr 05, 2008 12:04 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
I'm getting "uncategorized SQLException"s when adding or updating a Person object that has a one-to-one mapping with a User object and I'm not sure why. I've followed the directions located here for creating a one to one mapping with unique foreign key associations:
http://www.hibernate.org/hib_docs/reference/en/html/mapping.html

I imagine my situation is very common. Basically, I have a simple Users table with username & password for web security. Then I have a Person table that includes an Email field. The 2 tables are tied together because Users.username is also an email address. They look something like this:
Code:
CREATE TABLE Person (Id INT, Name VARCHAR(255), Email VARCHAR(255), UNIQUE (Email));
CREATE TABLE Users (Username VARCHAR(255) NOT NULL PRIMARY KEY, Password VARCHAR(255) NOT NULL, FOREIGN KEY (Username) REFERENCES Person (Email));


My shortened person.hbm.xml:
Code:
    <class name="people.Person" table="Person">
        <id name="id" column="Id">
            <generator class="native"/>
        </id>
        <property name="name" column="Name" length="255"/>
        <many-to-one name="user" class="account.User" column="Email" unique="true"/>
    </class>


My shortened users.hbm.xml:
Code:
    <class name="account.User" table="Users">
        <id name="username" column="Username">
            <generator class="assigned"/>
        </id>
        <property name="password" column="Password" length="255"/>
        <one-to-one name="person" class="people.Person" property-ref="user"/>
    </class>


I get a pretty strange result with the following code:
Code:
        log.debug("Saving person: " + person);
        sessionFactory.getCurrentSession().save(person);
        log.debug("Saved person: " + person);

before saving a particular person with an associated user, his id is 18. Afterwards, the saved person has id 19. Then when the transaction is committed I get that dreaded and uninformative "uncategorized SQLException" (root cause is equally uninformative "java.sql.BatchUpdateException: failed batch"). Interestingly this person object saves perfectly, maintaining his id of 18, if there is no associated user object.

Anybody have any ideas? How should I be implementing this one-to-one functionality?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 05, 2008 5:24 am 
Regular
Regular

Joined: Mon Mar 10, 2008 6:40 pm
Posts: 114
An update after messing with this issue for too long. I fixed the problem by making 2 changes:
  • moved the foreign key statement from the Users table to the Person table
  • added cascade="save-update" in the many-to-one and one-to-one elements


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.