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?