Hibernate version: 3.05 and 3.1.1
Configuration documents:
Code:
<hibernate-configuration>
<session-factory name="SessionFactory">
<property name="hibernate.connection.driver_class">
org.postgresql.Driver
</property>
<property name="hibernate.connection.password">
test
</property>
<property name="hibernate.connection.url">
jdbc:postgresql://localhost:5432/test
</property>
<property name="hibernate.connection.username">
test
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="show_sql">true</property>
<property name="hibernate.use_identifier_rollback">true</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
....
Mapping documents:Code:
<hibernate-mapping package="project.entity">
<class name="Patient" table="PATIENTS_T">
<id name="patientPk" column="PATIENT_PK">
<!-- Use the native generator,
in DB there must be auto increment mechanism -->
<generator class="native">
<param name="sequence">patients_t_patient_pk_seq</param>
</generator>
</id>
<property name="patientName" column="patient_name" type="string" not-null="true"/>
<property name="patientSex" column="patient_sex" type="string" not-null="true"/>
<many-to-one name="patientType" class="project.entity.lookup.PatientType" column="patient_type_fk" lazy="false" not-null="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Patient patient = null;
Transaction tx = null;
Session session = null;
try {
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
//Empty Object
patient = new Patient();
patient.setPatientName(name);
patient.setPatientSex(sex);
patient.setPatientType(null); //Will Cause Exception, in DB NOT NULL
session.save(patient); //Hibernate set the PK (from Postgres sequence, but Throw an Exception
tx.commit();
session.flush();
HibernateUtil.closeSession();
} catch (HibernateException e) {
tx.rollback();
session.evict(patient);
session.clear();
session.close();
e.printStackTrace();
out.println(tx.wasRolledBack());
}
//Primary Key still contains generated value from DB
System.out.println("Pat PK:"+patient.getPatientPk());
System.currentTimeMillis();
Name and version of the database you are using: INFO: RDBMS: PostgreSQL, version: 8.0.3
Debug level Hibernate log excerpt:
Hallo....
I have problem with re-saving an object which was rollback from a previous transaction.
The cause is that the all of the PK identifier contain the generated value from DB.
I've searched in the forum, and found some article, and some people just mention about the config:
hibernate.use_identifier_rollback
And I think it's set properly, since in the startup, the log shows:
Deleted entity synthetic identifier rollback: enabled
But still, using a debugger, the Primary Key is not set back after Transaction Rollback.
(My Code is provided above)
I even tried (desperately) using session.evict(), close(), clear(), but nothing helped. (Upgrade from 3.0.5 to 3.1.1 also didn't help).
Can anyone help me?? What did I do wrong?
About the
use_identifier_rollback, in Reference it's written:
when enabled, the generated identifier properties will be reset to
default values when
objects are deleted.
Objects are deleted->What does it mean???
default values -> I hope it's reset to the value before session.save(object)
Thank you very2 much for the explaination, help, anything which can help me.