Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hello,
I have two tables: user_info and user_login which have one-to-many relation, which means one user may have several login records. However no foreign key defined in tables because they are old production tables.
I defined a List property call "logins" in UserInfo class to store the login information, and I can successfully retrieve them. However, when I modified some properties in logins List, I had a problem updating the database. The error msg I got was:
11:06:40,991 ERROR SessionImpl:2399 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
Something very strange was: if I changed to MySQL database, and no changes to the code, I could do update successfully without any problem.
The driver I used for DB connection was: com.inet.tds.TdsDriver
Can anyone give me some tips? Thanks.
Hibernate version:
Hibernate 2.1.7c
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="org.hibernate.user">
<class name="UserInfo" table="user_info">
<id
column="user_id"
name="Id"
type="string"
>
<generator class="vm" />
</id>
<property
column="lang_preference"
length="3"
name="LangPreference"
not-null="false"
type="string"
/>
<property
column="address1"
length="40"
name="Address1"
not-null="false"
type="string"
/>
<property
column="birthdate"
length="23"
name="Birthdate"
not-null="false"
type="timestamp"
/>
<property
column="phone_home"
length="20"
name="PhoneHome"
not-null="false"
type="string"
/>
<property
column="address2"
length="40"
name="Address2"
not-null="false"
type="string"
/>
<property
column="username"
length="80"
name="Username"
not-null="false"
type="string"
/>
<property
column="vc_can_contact"
length="10"
name="VcCanContact"
not-null="false"
type="string"
/>
<property
column="phone_work"
length="20"
name="PhoneWork"
not-null="false"
type="string"
/>
<property
column="user_answer"
length="255"
name="UserAnswer"
not-null="false"
type="string"
/>
<property
column="zip"
length="10"
name="Zip"
not-null="false"
type="string"
/>
<property
column="business_name"
length="40"
name="BusinessName"
not-null="false"
type="string"
/>
<property
column="city"
length="30"
name="City"
not-null="false"
type="string"
/>
<property
column="send_reg_email"
length="40"
name="SendRegEmail"
not-null="false"
type="string"
/>
<property
column="phone_cell"
length="20"
name="PhoneCell"
not-null="false"
type="string"
/>
<property
column="hint"
length="40"
name="Hint"
not-null="false"
type="string"
/>
<property
column="lang_country"
length="3"
name="LangCountry"
not-null="false"
type="string"
/>
<property
column="member_since"
length="16"
name="MemberSince"
not-null="false"
type="date"
/>
<property
column="user_question_id"
length="10"
name="UserQuestionId"
not-null="false"
type="integer"
/>
<property
column="lastname"
length="30"
name="Lastname"
not-null="false"
type="string"
/>
<property
column="country"
length="40"
name="Country"
not-null="false"
type="string"
/>
<property
column="pword"
length="20"
name="Pword"
not-null="false"
type="string"
/>
<property
column="firstname"
length="30"
name="Firstname"
not-null="false"
type="string"
/>
<property
column="partner_can_contact"
length="10"
name="PartnerCanContact"
not-null="false"
type="string"
/>
<property
column="phone_fax"
length="20"
name="PhoneFax"
not-null="false"
type="string"
/>
<property
column="state"
length="5"
name="State"
not-null="false"
type="string"
/>
<property
column="email"
length="80"
name="Email"
not-null="false"
type="string"
/>
<property
column="owner_organization_no"
length="10"
name="OwnerOrganizationNo"
not-null="true"
type="integer"
/>
<bag name="logins" table="user_login" lazy="true" inverse="true" cascade="save-update">
<key column="user_id"/>
<one-to-many class="org.hibernate.user.UserLogin"/>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="org.hibernate.user">
<class name="UserLogin" table="user_login">
<composite-id class="UserLoginPK" name="Id">
<key-property
column="user_id"
name="UserId"
type="string"
/>
<key-property
column="intime"
name="Intime"
type="integer"
/>
<key-property
column="datetime"
name="Datetime"
type="timestamp"
/>
</composite-id>
<property
column="lasttime"
length="20"
name="Lasttime"
not-null="false"
type="integer"
/>
<property
column="endtime"
length="20"
name="Endtime"
not-null="false"
type="integer"
/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Transaction tx=null;
try {
tx = s.beginTransaction();
List list = s.createCriteria(UserInfo.class)
.add( Expression.eq("id", "664413804") )
.setFetchMode("logins", FetchMode.EAGER)
.list();
if (list.size()==0) throw new IllegalArgumentException("No user for the given id: " + "664413804");
user = (UserInfo) list.get(0);
System.out.println(
"User: " + user.getId() + " - " + user.getFirstname() +
" - " + user.getLastname() );
System.out.println();
System.out.println(
"Login information: " + user.getLogins() );
System.out.println();
List logins = user.getLogins();
UserLogin login = (UserLogin)logins.iterator().next();
login.setLasttime(new Integer(0));
user.setLastname("Delayyyy");
s.save(user);
tx.commit();
}
Full stack trace of any exception that occurs:
Hibernate: update user_login set lasttime=?, endtime=? where user_id=? and intime=? and datetime=?
11:06:40,991 ERROR SessionImpl:2399 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:663)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:623)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at org.hibernate.user.Main.changeUserDetails(Main.java:120)
at org.hibernate.user.Main.main(Main.java:149)
Exception in thread "main"
Name and version of the database you are using:
Microsoft SQL Server 8.0
The generated SQL (show_sql=true):
Hibernate: update user_login set lasttime=?, endtime=? where user_id=? and intime=? and datetime=?