For tables:
users_main{
user_id, user_name, user_email }
tokens_data{
token_id, secret_a, secret_b
}
token_user_mapping {
token_id, user_id
}
A have this reletion:
tables user_main AND token_user_maping ---> 1 to N
token_user_mapping to tokens_data --> 1 to 1
How should look mapping file?
Hibernate version:
Hibernate 2.1.6
Mapping documents:
<hibernate-mapping>
<class name="com.server.dbcomm.model.User" table="users_main" >
<id name="userIdMain" type="java.lang.Integer" column="user_id_main">
<generator class="assigned" />
</id>
<version name="version" column="version" unsaved-value="null" />
<property
name="propertyKeys"
type="java.lang.String"
column="property_keys"
length="255"
/>
<property
name="propertyValues"
type="java.lang.String"
column="property_values"
length="255"
/>
<property
name="userName"
type="java.lang.String"
column="user_name"
length="16"
/>
<property
name="userEmail"
type="java.lang.String"
column="user_email"
length="30"
/>
<set name="tokenMappings" table="token_user_mapping" cascade="save-update" lazy="true">
<key column="user_id_main"/>
<many-to-many column="token_id" class="com.server.dbcomm.model.Token"/>
</set>
</class>
<class name="com.server.dbcomm.model.Token" table="tokens_data" dynamic-update="true" lazy="true">
<meta attribute="class-description" inherit="false">
Token
</meta>
<id name="tokenId" type="java.lang.Integer" column="token_id" >
<generator class="assigned" />
</id>
<version name="version" column="version" unsaved-value="null"/>
<property
name="secretA"
type="java.lang.String"
column="secret_a"
not-null="true"
length="128"
/>
<property
name="secretB"
type="java.lang.String"
column="secret_b"
not-null="true"
length="128"
/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
I use Spring framework:
public void testUser () throws Exception {
User u1 = new User(new Integer(1), "sto1", "sto_sto1@stoian.org",
UserDAO userDAO = (UserDAO)context.getBean("UserDAO");
userDAO.makePersistent(u1); //exeption is trown here
}
//HibernateDaoSupport is class from Spring framework. It conteints method getHibernateTemplate()
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
is this method ok?
public void addTokenForUser (Token token, User user) throws DataAccessException {
user.addToken(token);
update(user);
}
public void makePersistent (User user) throws DataAccessException {
getHibernateTemplate().saveOrUpdate(user);
}
public void update (User user) throws DataAccessException {
getHibernateTemplate().update(user);
}
}//end of class
Full stack trace of any exception that occurs:
2004-10-18 11:56:36 net.sf.hibernate.StaleObjectStateException <init>
WARNING: An operation failed due to stale data
net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by anothe
r transaction (or unsaved-value mapping was incorrect) for com.server.dbcomm.mod
el.User instance with identifier: 1
at net.sf.hibernate.persister.AbstractEntityPersister.check(AbstractEnti
tyPersister.java:506)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:687)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52
)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.j
ava:61)
at org.springframework.orm.hibernate.HibernateTransactionManager.doCommi
t(HibernateTransactionManager.java:460)
at org.springframework.transaction.support.AbstractPlatformTransactionMa
nager.commit(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.
doCommitTransactionAfterReturning(TransactionAspectSupport.java:241)
at org.springframework.transaction.interceptor.TransactionInterceptor.in
voke(TransactionInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:138)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynami
cAopProxy.java:152)
at $Proxy0.makePersistent(Unknown Source)
at com.server.test.UserTest.testUser(UserTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at com.server.test.TestCase.runTest(TestCase.java:34)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at junit.textui.TestRunner.run(TestRunner.java:72)
at com.server.test.UserTest.main(UserTest.java:70)
Name and version of the database you are using:
MySQL 4.0.21
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
_________________ w.a.s.p
|