I am still a newbie attempting to get the Cat example program running with an Oracle9i database.
The following code:
// Initialize Hibernate (Configuration and SessionFactory)
initHibernate();
// Prepare out
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Create some Cats
beginTransaction();
createCats(out);
endTransaction(true);
public void createCats(PrintWriter out)
throws HibernateException {
Cat princess = new Cat();
princess.setType('W');
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);
Cat max = new Cat();
max.setType('D');
max.setSex('M');
max.setWeight(8.1f);
session.save(max);
Cat sophie = new Cat();
sophie.setType('D');
sophie.setSex('F');
sophie.setWeight(4.1f);
session.save(sophie);
}
private void initHibernate()
throws HibernateException {
// Load Configuration and build SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}
private void beginTransaction()
throws HibernateException {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
private void endTransaction(boolean commit)
throws HibernateException {
if (commit) {
transaction.commit(); <==== Exception occurs here
} else {
// Don't commit the transaction, can be faster for read-only operations
transaction.rollback();
}
session.close();
}
==========================================
public class Cat {
private long id;
private String color;
private char sex, type;
private float weight;
public Cat() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
==========================================
<hibernate-mapping>
<class name="ex.Cat" table="CATS" discriminator-value="W">
<id name="id" column="id" type="int">
<generator class="sequence"> <param name="sequence">id_seq</param> </generator>
</id>
<discriminator column="type" type="character"/>
<property name="color" not-null="true"/>
<property name="sex" not-null="true" update="false"/>
<property name="weight"/>
<subclass name="ex.DomesticCat" discriminator-value="D">
<property name="name" column="name" type="string"/>
</subclass>
</class>
==========================================
SQL> desc cats;
Name Null? Type
----------------------------------------- -------- ------------------
ID NOT NULL NUMBER(1)
SEX NOT NULL NUMBER(1)
WEIGHT NOT NULL NUMBER(1)
NAME VARCHAR2(30)
SQL> select * from user_sequences;
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY C O CACHE_SIZE
------------------------------ ---------- ---------- ------------ - - ----------
LAST_NUMBER
-----------
ID_SEQ 1 1.0000E+27 1 N N 20
41
==========================================
Generates this exception:
net.sf.hibernate.HibernateException: identifier of an instance of ex.Cat altered from 16 to 16
void net.sf.hibernate.impl.SessionImpl.flushEntities()
SessionImpl.java:2141
void net.sf.hibernate.impl.SessionImpl.flushEverything()
SessionImpl.java:2017
void net.sf.hibernate.impl.SessionImpl.flush()
SessionImpl.java:2004
void net.sf.hibernate.transaction.JDBCTransaction.commit()
JDBCTransaction.java:57
void ex.TestServlet.endTransaction(boolean)
TestServlet.java:131
I looked through the source code in SessionImpl.java and I am not sure why this is going off
if ( !entry.id.equals(oid) ) throw new HibernateException(
"identifier of an instance of " +
persister.getClassName() +
" altered from " +
entry.id +
" to " + oid
);
Since the value is 16 in both cases
Thanks, pugmaster
|