Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Sequence Generating with MappedSuperclass id(Sequence Generating with MappedSuperclass id)
Sequence Generating with MappedSuperclass id [message #1385480] Fri, 06 June 2014 18:54
Jacob Champlin is currently offline Jacob ChamplinFriend
Messages: 3
Registered: July 2009
Junior Member
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?
Previous Topic:Query on Saveable Helper
Next Topic:MOXy failes to serialize list of objects
Goto Forum:
  


Current Time: Tue Mar 19 11:53:59 GMT 2024

Powered by FUDForum. Page generated in 0.01766 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top