Try this...
Mapping:
Code:
<hibernate-mapping default-cascade="all">
<class name="foo" table="foos">
<composite-id name="id" class="CompositeID" unsaved-value="any">
<key-property name="sequenceId" type="java.lang.Long" column="sequence_id"/>
<key-property name="stringId" type="string" column="string_id"/>
</composite-id>
</class>
....
</hibernate-mapping>
A key generator:
Code:
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.id.SequenceGenerator;
import net.sf.hibernate.type.LongType;
import java.util.Properties;
public class CompositeIDGenerator {
private static SequenceGenerator SEQUENCE_GENERATOR =
new SequenceGenerator();
static {
try {
SEQUENCE_GENERATOR.configure(
new LongType(),
new Properties(),
((SessionFactoryImplementor)<!-- INSERT METHOD TO GET YOUR SESSION FACTORY HERE -->).getDialect()
);
} catch(HibernateException he) {
//You'll want to do a bit more error checking than this, of course... :)
he.printStackTrace();
}
}
public static final CompositeSiteID generate(
String stringId,
Session session
)
throws HibernateException {
CompositeID id = new CompositeID();
id.setStringId(stringId);
id.setSequenceId((Long)SEQUENCE_GENERATOR.generate(
(SessionImplementor)session,
id
)
);
return id;
}
}
Then, whenever you create an object of type foo, you've got to do this:
Code:
new Foo(CompositeIDGenerator.generate("A_STRING", session), ...)