Hi!
I'm trying to build a simple one-to-one relation in my application. After
some hours of trial and error it nearly works, but a last problem is still
unsolved.
I reduced the whole thing to a simple example:
Class User has a one-to-one relation to class Stats:
Code:
public class User {
// CREATE TABLE users ( id BIGINT NOT NULL,
// name CHAR(30),
// PRIMARY KEY(id))
private long id;
private String name;
private Stats stats;
public User(){ }
[...] // getters and setters for all attributes
}
public class Stats {
// CREATE TABLE stats ( userid BIGINT NOT NULL,
// value INT ,
// PRIMARY KEY(userid),
// FOREIGN KEY(userid) REFERENCES users(id) )
private long id;
private int value;
private User user;
public Stats(){ }
[...] // getters and setters for all attributes
}
So there is a bidirectional relation between these two classes.
User uses a TableHiLoGenerator for creating the primary key (id).
The PK of Stats (which is also a foreign key to Users) shall be the same
as the associated User object.
Here's the mapping-setup:
Code:
<hibernate-mapping package="persistence">
<class name="User" table="users" proxy="User" dynamic-update="true">
<id name="id" type="long" column="id" unsaved-value="0">
<generator class="org.hibernate.id.TableHiLoGenerator">
<param name="table">uid_table</param>
<param name="column">next_hi_value_column</param>
</generator>
</id>
<property name="name" column="`name`" />
<one-to-one name="Stats"
class="persistence.Stats"
cascade="all"
foreign-key="userid"
constrained="true" />
</class>
</hibernate-mapping>
-----------------------------------------
<hibernate-mapping package="persistence">
<class name="Stats" table="stats" proxy="Stats" dynamic-update="true">
<id name="id" column="userid">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="User" constrained="true" cascade="all"/>
<property name="value" column="`value`"/>
</class>
</hibernate-mapping>
Next is a short program to test the design:
Code:
public static void main(String[] args) {
SessionFactory sessions = new Configuration().configure().buildSessionFactory();
User u = new User();
u.setName("test");
Stats s = new Stats();
u.setStats(s);
s.setUser(u);
Session ses = sessions.openSession();
Transaction tx = ses.beginTransaction();
Object id = ses.save(u);
tx.commit();
ses.close();
}
And finally the output of the test-program:
Code:
25.12.2004 23:11:03 org.hibernate.impl.SessionFactoryImpl checkNamedQueries
INFO: Checking 0 named queries
Hibernate: insert into users (`name`, id) values (?, ?)
Hibernate: update stats set `value`=? where userid=?
25.12.2004 23:11:03 org.hibernate.jdbc.BatchingBatcher doExecuteBatch
FATAL: Exception executing batch:
org.hibernate.HibernateException: Batch update row count wrong: 0
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:64)
...
First hibernate inserts the newly created User into the database. That's good. But then it tries to update a stats-entry which doesn't exist yet.
There should be a "insert into stats..", not an update.
Where's the mistake? What should I do?
I'm using hibernate 3.0 beta 1.
Thank you for your help.
zinfandel