Hi,
I have a use case where I need to generate a custom counter. The case is where I need to place orders and generate an order number.
I have written a code generator class
Code:
package filters.controllers;
import java.io.Serializable;
import javax.persistence.Query;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;
import play.db.jpa.JPA;
public class CodeGenerator implements IdentifierGenerator
{
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException
{
String prefix = "BAKR";
String code = null;
Query query = JPA.em().createNativeQuery(
"SELECT nextval ('s_bkg1') as nextval"); //this is a Postgres sequence created manually by hand
String result = String.valueOf(query.getResultList().get(0));
code = prefix + "00" + result;
return code;
}
}
In my model I have called it
Code:
public class Booking
{
@Id
public int trans_pid;
@GenericGenerator(name="bakr", strategy="filters.controllers.CodeGenerator")
@GeneratedValue(generator="bakr")
public String booking_no;
and I am not setting the same through code.
I want to generate order nos as BAKR001,BAKR002, ..However I am getting NULLS inserted in the database.
Please could somebody assist.
-S