问题描述
通常Id列如下,它可以很好地工作并生成序列值
Normally Id column is as follows and it works perfectly fine and generated sequence values
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idGenerator")
@SequenceGenerator(name = "idGenerator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 50)
@Column(name = "ID")
public Long getId()
{ return mId; }
现在我想要的是,如果在程序中设置xyz.setId(200),它将ID保存为200而不是序列生成的ID,那么我该如何实现呢?我也想同时使用两个属性sequenceName ="HIBERNATE_SEQUENCE",allocationSize = 50.
Now what I want is if in program I set xyz.setId(200) it should save Id as 200 instead of one generated by sequence.Now how can I acheive this? I also want to use both attributes sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 50.
推荐答案
最后经过1天的使用时间,我挖了罐子,得到了解决方案.我用@GenericGenerator(name = "idGenerator", strategy = "com.jayash.domain.UseExistingOrGenerateIdGenerator")
Finally after 1 day of utilized time I digged into the jar and I got the solution.I used @GenericGenerator(name = "idGenerator", strategy = "com.jayash.domain.UseExistingOrGenerateIdGenerator")
public class UseExistingOrGenerateIdGenerator extends SequenceHiLoGenerator {
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);
return id != null ? id : super.generate(session, object);
}
@Override
public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
params.put(org.hibernate.id.SequenceGenerator.SEQUENCE, "HIBERNATE_SEQUENCE");
params.put(SequenceHiLoGenerator.MAX_LO, String.valueOf("49"));
super.configure(type, params, dialect);
}
}
Yipee的工作很棒
And Yipee Its working great
这篇关于如何覆盖@SequenceGenerator(name ="idGenerator",sequenceName ="HIBERNATE_SEQUENCE",allocationSize = 50)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!