java - Hibernate generate ID from database function -
my code is
`@id @genericgenerator(name="generator", strategy="increment") @generatedvalue(generator="generator") @column(name = "pm_id", nullable = false, length=12) private long pmid;` in above id genarate max id +1 database want generate pmid column database function , want pass value function. function name generateid(2,3)
so please tell me how this..
you can use custom id generator call stored procedure.
define @id point custom generator :
@id @genericgenerator(name="mycustomgenerator", strategy="org.mytest.entity.customidgenerator" ) @generatedvalue(generator="mycustomgenerator" ) @column(name = "pm_id", nullable = false, length=12) private long pmid; private integer field1; private integer field2; .... getters , setters .... and create custom generator implementing identifiergenerator , passing properties of entity stored proc parameters:
public class customidgenerator implements identifiergenerator { private static final string query_call_store_proc = "call generateid(?,?,?)"; public serializable generate(sessionimplementor session, object object) throws hibernateexception { long result = null; try { connection connection = session.connection(); callablestatement callablestmt = connection. preparecall(query_call_store_proc); callablestmt.registeroutparameter(1, java.sql.types.bigint); callablestmt.setint(2, ((myobject) object).getfield1()); callablestmt.setint(3, ((myobject) object).getfield2()); callablestmt.executequery(); // result out parameter #1 result = callablestmt.getlong(1); connection.close(); } catch (sqlexception sqlexception) { throw new hibernateexception(sqlexception); } return result; } }
Comments
Post a Comment