Hi,
I am using a composite key in my project mapped to the class ID, this class has two variables id and dataOwner. id is an auto_incremented field, and dataOwner will be a field set by the application.
I was trying to modify this example for my code
http://forum.hibernate.org/viewtopic.php?t=927648
and after saving the newly created object, the returned object did not have any value for the ID.id field. Was there something wrong in what I did? Here's the files I'm using for all this.
Thanks for any help you can give.
Somsack
Hibernate version:
Hibernate version 2.1.6
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="com.crisys.sf.dto.NextStatus"
table="NEXT_STATUS"
>
<composite-id name="comp_id" class="com.crisys.sf.dto.ID">
<key-property
name="id"
column="id"
type="java.lang.Integer"
length="10" />
<key-property
name="dataOwner"
column="dataOwner"
type="java.lang.Integer"
length="10" />
</composite-id>
<!-- associations -->
<!-- bi-directional many-to-one association to UnitStatusType -->
<many-to-one
name="fromUnitStatusType"
class="com.crisys.sf.dto.UnitStatusType"
not-null="true"
>
<column name="fromStatusTypeID" />
<column name="fromStatusTypeDataOwnerID" />
</many-to-one>
<many-to-one
name="toUnitStatusType"
class="com.crisys.sf.dto.UnitStatusType"
not-null="true"
>
<column name="toStatusTypeID" />
<column name="toStatusTypeDataOwnerID" />
</many-to-one>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Code:
ID ret = null;
if (null==dto.getId().getId()) {
logger.finest(" saving the dto test 1");
dto.setId(CompositeIDGenerator.generate(new Integer("999"),
session));
logger.finest(" saving dto.setId="+dto.getId());
ret = (ID) session.save(dto);
logger.finest(" after session.save="+dto.getId());
logger.finest(" after session.save ret="+ret);
dto.setId(CompositeIDGenerator.generate(new Integer("999"),
session));
logger.finest(" after dto.setId 2="+dto.getId());
}
Here's the code to the CompositeIDGenerator
Code:
public class CompositeIDGenerator {
private static Logger logger = Logger.getLogger("CompositeIDGenerator");
private static IdentityGenerator SEQUENCE_GENERATOR = new IdentityGenerator();
private static boolean dialect = false;
public static final ID generate(Integer dataOwner, Session session) {
logger.config("Start generate");
ID id = new ID();
id.setDataOwner(dataOwner);
try {
id.setId((Integer) SEQUENCE_GENERATOR.generate(
(SessionImplementor) session, id));
logger.finest(" generated id="+id);
} catch (HibernateException e) {
logger.severe("HibernateException from id.setID");
e.printStackTrace();
} catch (SQLException e) {
logger.severe("SQLException from id.setID");
e.printStackTrace();
}
logger.finest(" before return id="+id);
return id;
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
mysql Ver 12.21 Distrib 4.0.14
The generated SQL (show_sql=true):
15:51:31,731 INFO [STDOUT] Hibernate: insert into NEXT_STATUS (fromStatusTypeID, fromStatusTypeDataOwnerID, toStatusTypeID, toStatusTypeDataOwnerID, id, dataOwner) values (?, ?, ?, ?, ?, ?)
Debug level Hibernate log excerpt:
15:51:31,813 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
FINER: after committing transaction
15:51:31,815 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
FINER: after flushing session
15:51:31,823 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
FINER: after closing session 2
15:51:31,824 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
FINEST: ret=com.crisys.sf.dto.ID@d704f0[id=<null>,dataOwner=999]
15:51:31,826 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
FINER: Id of persisted DTO: com.crisys.sf.dto.ID@11d565f[id=<null>,dataOwner=999]
15:51:31,851 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
FINER: Id.getId of persisted DTO: null
15:51:31,851 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
FINER: Id.getDataOwner of persisted DTO: 999
15:51:31,852 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy persistRecord
CONFIG: end persist
15:51:31,852 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy publishMessage
CONFIG: begin publishMessage
15:51:31,853 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy publishMessage
FINER: publishing to COM.CRISYS.SF.DTO.NEXTSTATUS
15:51:31,853 ERROR [STDERR] Oct 12, 2004 3:51:31 PM com.crisys.sf.framework.dataAccess.HibernateProxy publishMessage
FINEST: ID of published dto=com.crisys.sf.dto.ID@11d565f[id=<null>,dataOwner=999]