我写了自己的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")
我会非常感谢任何解决方案!!!!
非常感谢,Norbert
答案 0 :(得分:6)
您可以通过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 create sequence 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;
}
}
答案 1 :(得分:1)
@Id
@GenericGenerator(name = "seq_id", strategy = "de.generator.AkteIdGenerator")
@GeneratedValue(generator = "seq_id")
@Column(name = "ID")
private Integer Id;
http://blog.anorakgirl.co.uk/2009/01/custom-hibernate-sequence-generator-for-id-field/
答案 2 :(得分:1)
不确定这是否有帮助,但是我在寻找答案的过程中一直遇到这篇文章,尽管找不到答案,但我自己找到了解决方案。所以我认为这可能是最好的分享场所。
如果您将休眠用作JPA提供程序,则可以手动调用分配给给定实体类的ID生成器。首先注入JpaContext:
@Autowired
org.springframework.data.jpa.repository.JpaContext jpaContext;
然后使用以下命令获取内部的org.hibernate.id.IdentifierGenerator:
org.hibernate.engine.spi.SessionImplementor session = jpaContext.getEntityManagerByManagedType(MyEntity.class).unwrap(org.hibernate.engine.spi.SessionImplementor.class);
org.hibernate.id.IdentifierGenerator generator = session.getEntityPersister(null, new MyEntity()).getIdentifierGenerator();
现在您可以通过编程方式从生成器获取ID:
Serializable id = generator.generate(session, new MyEntity());
答案 3 :(得分:0)
您的帖子有助于更新序列的名称。
因为我每月使用一个序列,并且配置不会更新每个标识符生成。
这是我的代码:
@Override
public Serializable generate(SessionImplementor sessionImplementator,
Object object) throws HibernateException {
Calendar now = Calendar.getInstance();
// If month sequence is wrong, then reconfigure.
if (now.get(Calendar.MONTH) != SEQUENCE_DATE.get(Calendar.MONTH)) {
super.configure(new LongType(), new Properties(),
sessionImplementator.getFactory().getDialect());
}
Long id = (Long) super.generate(sessionImplementator, object);
String sId = String.format("%1$ty%1$tm%2$06d", SEQUENCE_DATE, id);
return Long.parseLong(sId);// 1301000001
}