So today I thought I would try to convert one of our Hibernate Projects to EclipseLink and within 5 minutes I got stuck.
In Hibernate we have a @MappedSuperclass with:
@Id
@GeneratedValue(generator="postgres-seq")
@GenericGenerator(name="postgres-seq", strategy = "com.mycompany.hibernate.PostgresSequenceGenerator")
@Column(name = "id", nullable = false)
public Long getId() { return this.id; }
public void setId(Long id) { this.id = id; }
We do this because we wanted to force the same id on all our models.
We have created our own sequence generator that reflects on the object being persisted to construct the name of the PostgreSql sequence.
public class PostgresSequenceGenerator extends SequenceGenerator {
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
if (params.getProperty(SEQUENCE) == null || params.getProperty(SEQUENCE).length() == 0) {
String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
String pkName = params.getProperty(PersistentIdentifierGenerator.PK);
if (tableName != null) {
String seqName = tableName + "_" + pkName + "_seq";
params.setProperty(SEQUENCE, seqName);
}
}
super.configure(type, params, dialect);
}
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
if (object.getClass().isAnnotationPresent(ForcedId.class)) {
return session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);
}
else {
return super.generate(session, object);
}
}
}
I have tried every EclipseLink hook I can find for sequences. I think extending StandardSequence is the EclipseLink way, however nothing is passed in that allows me to reflect on the Object that needs the sequence.
Can someone point me in the right direction?