java – Hibernate:手动调用SequenceGenerator?

前端之家收集整理的这篇文章主要介绍了java – Hibernate:手动调用SequenceGenerator?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了自己的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 ...
   }
}

有谁知道如何从我的生成器类调用sequenceGenerator,我通常可以根据注释定义:

@GeneratedValue(
        strategy = GenerationType.SEQUENCE,generator = "MY_SEQUENCE")
@SequenceGenerator(
        allocationSize = 1,name = "MY_SEQUENCE",sequenceName = "MY_SEQUENCE_NAME")

我会非常感谢任何解决方案!!!!

非常感谢,诺伯特

解决方法

您可以通过Generator类调用SequenceGenerator.通过编写此代码.
自定义生成器类应该是
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);

}

}
在上面的代码中,Stud_NoSequence是可以创建的序列名称.在数据库中通过wring创建序列Stud_NoSequence;
Hibernate.String是SequenceGenerator类将返回的类型.

而域类将是

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;
}
    }
原文链接:https://www.f2er.com/java/128515.html

猜你在找的Java相关文章