Hello,
I'm experiencing the following problem:
I update a property value of a persistent object in my code,
but the appropriate value is not updated in the database.
I went through the documentation & forum but couldn't find anything similar there.
I'll really appreciate any help!
Following the details:
Hibernate version:
3.2.3.ga
Mapping documents:
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://IP/DB;charset=cp1255</property>
<property name="connection.instance">SQLEXPRESS</property>
<property name="connection.username">USER</property>
<property name="connection.password">PWD</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping resource="path/to/file/Class2.hbm.xml"/>
<mapping resource="path/to/file/Class1.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Class1.cfg.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="path.to.class.Class1" table="table1" lazy="false">
<composite-id>
<key-property name="id1" column="id1"/>
<key-many-to-one name="foreignKey" class="path.to.class.Class2" column="foreign_key" lazy="false"/>
<key-property name="id2" column="id2"/>
</composite-id>
</class>
</hibernate-mapping>
Class2.cfg.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="path.to.class.Class2" table="table2">
<id name="id" column="id">
<generator class="org.hibernate.id.enhanced.TableGenerator">
<param name="segment_value">sequence1</param>
<param name="initial_value">1</param>
</generator>
</id>
<property name="property1" column="property1"/>
<property name="property2" column="property2"/>
<property name="property3" column="property3"/>
<property name="property4" column="property4"/>
<property name="property5" column="property5"/>
</class>
</hibernate-mapping>
HibernateUtil.java:
public class HibernateUtil
{
private static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Code between sessionFactory.openSession() and session.close():
Transaction tr;
tr = HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
List objects = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from Class1 where id1=11").list();
Class1 object1 = (Class1) objects.get(0);
object1.setid2(new Integer(6)); //the appropriate property is changed here, but this change is not reflected in the DB
HibernateUtil.getSessionFactory().getCurrentSession().flush();
tr.commit();
Full stack trace of any exception that occurs:
-
Name and version of the database you are using:
SQL Server 2005
The generated SQL (show_sql=true):
Hibernate: select grouphiera0_.id1 as object11_2_, grouphiera0_.foreign_key as asset2_2_, grouphiera0_.id2 as object13_2_ from table1 grouphiera0_ where grouphiera0_.id1=11
Hibernate: select assetgroup0_.id as id1_0_, assetgroup0_.property1 as name2_1_0_, assetgroup0_.property2 as descript3_1_0_, assetgroup0_.property3 as conversion4_1_0_, assetgroup0_.property4 as target5_1_0_, assetgroup0_.property5 as accepted6_1_0_ from table2 assetgroup0_ where assetgroup0_.id=?
Hibernate: select assetgroup0_.id as id1_0_, assetgroup0_.property1 as name2_1_0_, assetgroup0_.property2 as descript3_1_0_, assetgroup0_.property3 as conversion4_1_0_, assetgroup0_.property4 as target5_1_0_, assetgroup0_.property5 as accepted6_1_0_ from table2 assetgroup0_ where assetgroup0_.id=?
Hibernate: select assetgroup0_.id as id1_0_, assetgroup0_.property1 as name2_1_0_, assetgroup0_.property2 as descript3_1_0_, assetgroup0_.property3 as conversion4_1_0_, assetgroup0_.property4 as target5_1_0_, assetgroup0_.property5 as accepted6_1_0_ from table2 assetgroup0_ where assetgroup0_.id=?
Hibernate: select assetgroup0_.id as id1_0_, assetgroup0_.property1 as name2_1_0_, assetgroup0_.property2 as descript3_1_0_, assetgroup0_.property3 as conversion4_1_0_, assetgroup0_.property4 as target5_1_0_, assetgroup0_.property5 as accepted6_1_0_ from table2 assetgroup0_ where assetgroup0_.id=?
Hibernate: select assetgroup0_.id as id1_0_, assetgroup0_.property1 as name2_1_0_, assetgroup0_.property2 as descript3_1_0_, assetgroup0_.property3 as conversion4_1_0_, assetgroup0_.property4 as target5_1_0_, assetgroup0_.property5 as accepted6_1_0_ from table2 assetgroup0_ where assetgroup0_.id=?
Hibernate: select assetgroup0_.id as id1_0_, assetgroup0_.property1 as name2_1_0_, assetgroup0_.property2 as descript3_1_0_, assetgroup0_.property3 as conversion4_1_0_, assetgroup0_.property4 as target5_1_0_, assetgroup0_.property5 as accepted6_1_0_ from table2 assetgroup0_ where assetgroup0_.id=?
Thanks Allot,
Vitaliy
|