MappedSuperclass – Change SequenceGenerator in Subclass

Yes, it is possible. You can override the default generator name with the @SequenceGenerator annotation. Base class @MappedSuperclass public abstract class PersistentEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = “default_gen”) protected Long id = 0L; public Long getId() { return id; } public void setId(Long id) … Read more

get next sequence value from database using hibernate

You can use Hibernate Dialect API for Database independence as follow class SequenceValueGetter { private SessionFactory sessionFactory; // For Hibernate 3 public Long getId(final String sequenceName) { final List<Long> ids = new ArrayList<Long>(1); sessionFactory.getCurrentSession().doWork(new Work() { public void execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData()); PreparedStatement preparedStatement = … Read more

Sequential Guid Generator

You could just use the same Win32 API function that SQL Server uses: UuidCreateSequential and apply some bit-shifting to put the values into big-endian order. And since you want it in C#: private class NativeMethods { [DllImport(“rpcrt4.dll”, SetLastError=true)] public static extern int UuidCreateSequential(out Guid guid); } public static Guid NewSequentialID() { //Code is released into … Read more

How does the JPA @SequenceGenerator annotation work

sequenceName is the name of the sequence in the DB. This is how you specify a sequence that already exists in the DB. If you go this route, you have to specify the allocationSize which needs to be the same value that the DB sequence uses as its “auto increment”. Usage: @GeneratedValue(generator=”my_seq”) @SequenceGenerator(name=”my_seq”,sequenceName=”MY_SEQ”, allocationSize=1) If … Read more