Java – Hibernate: manually call sequencegenerator?
•
Java
I wrote my idgenerator:
public class AkteIdGenerator implements IdentifierGenerator { public Serializable generate(SessionImplementor session,Object object) throws HibernateException { // if custom id is set -> use this id if (object instanceof SomeBean) { SomeBean someBean = (SomeBean) object; Long customId = someBean.getCustomId(); if (customId != 0) { return customId; } } // otherwise --> call the SequenceGenerator manually SequenceStyleGenerator sequenceGenerator ... } }
Who knows how to call sequencegenerator from my generator class? I can usually define it according to comments:
@GeneratedValue( strategy = GenerationType.SEQUENCE,generator = "MY_SEQUENCE") @SequenceGenerator( allocationSize = 1,name = "MY_SEQUENCE",sequenceName = "MY_SEQUENCE_NAME")
I would appreciate any solution!!!!
Thank you very much, Norbert
Solution
You can call sequencegenerator.com through the generator class By writing this code
public class StudentNoGenerator implements IdentifierGenerator { public Serializable generate(SessionImplementor session,Object object)throws HibernateException { SequenceGenerator generator=new SequenceGenerator(); Properties properties=new Properties(); properties.put("sequence","Stud_NoSequence"); generator.configure(Hibernate.STRING,properties,session.getFactory().getDialect()); return generator.generate(session,session); }
}In the above code, student_ Nosequence is the name of the sequence that can be created Create a sequence study in the database by wring_ NoSequence; Hibernate. String is the type that the sequencegenerator class will return
The domain class will be
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity @org.hibernate.annotations.GenericGenerator( name = "Custom-generator",strategy = "com.ssis.id.StudentNoGenerator" ) public class Student { @Id @GeneratedValue(generator = "Custom-generator") String rno; @Column String name; public String getRno() { return rno; } public void setRno(String rno) { this.rno = rno; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码