我想使用hibernate工具生成序列(pojo to sql)。当然它工作正常。
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
@SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ" )
@Column(name="id")
public Long getId() {
return id;
}
此代码生成以下sql
create sequence RTDS_ADSINPUT_SEQ;
问题是我想指定像
这样的属性INCREMENT BY,NOCACHE CYCLE
最后的ddl脚本应该是下面的东西
CREATE SEQUENCE RTDS_ADSINPUT_SEQ MINVALUE 1 MAXVALUE
999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE ORDER CYCLE ;
但到目前为止,我看到hibernate只支持name, sequncename,allocation,initialvalue
如果我可以在pojo中包含这些属性作为注释,请告诉我。
答案 0 :(得分:7)
我在Hibernate源代码中查找了它(4.2.7)。无法使用注释(JPA和Hibernate)来指定它。
但是,您可以提供自己的方言来实现这一目标。
public class MyOwnOracleDialect extends Oracle10gDialect {
@Override
protected String getCreateSequenceString(final String sequenceName, final int initialValue, final int incrementSize)
throws MappingException {
String createSequenceString = super.getCreateSequenceString(sequenceName, initialValue, incrementSize);
return createSequenceString + " NOCACHE ORDER CYCLE"; // modify this string as you like
}
}
拥有这样的实体
@Entity
public class MyEntity {
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
@SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ", allocationSize = 1, initialValue = 0)
@Column(name="id")
private Long id;
// ...
}
您可以按照Hibernate doc(http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional-dialects)
中的说明设置新的Dialect答案 1 :(得分:0)
我认为你正在寻找像这样的东西
<id name="pk_field" column="column_name">
<generator class="sequence">
<param name="sequence">sequence_name</param>
<param name="parameters">START WITH 5 INCREMENT BY 10</param>
</generator>
</id>