问题描述
我想用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
创建序列RTDS_ADSINPUT_SEQ;
问题是我想指定属性如$ / b
INCREMENT BY,NOCACHE CYCLE
和最终的ddl脚本应该是下面的一些东西
CREATE SEQUENCE RTDS_ADSINPUT_SEQ MINVALUE 1 MAXVALUE
999999999999999999增加1开始1个NOCACHE命令CYCLE;
但是据我看到hibernate只支持名称,sequncename,分配,initialvalue
请指教我是否可以在pojo中包含这些属性作为注释。
我在Hibernate源代码(4.2.7)中查了一下。不能用注释来指定它(既不是JPA也不是Hibernate)。
然而,你可以提供你自己的方言来实现这一点。
public class MyOwnOracleDialect extends Oracle10gDialect {
$ b @Override
protected String getCreateSequenceString(final String sequenceName,final int initialValue,final int incrementSize)
抛出MappingException {
String createSequenceString = super.getCreateSequenceString(sequenceName,initialValue,incrementSize);
return createSequenceString +NOCACHE ORDER CYCLE; //像这样修改这个字符串
code $
像这样
@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)
私人长ID;
// ...
}
您可以按照Hibernate doc中的描述设置您的新Dialect()
I wanted to to generate sequence using hibernate tool ( pojo to sql). And definitely it works fine.
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqid-gen")
@SequenceGenerator(name = "seqid-gen", sequenceName = "RTDS_ADSINPUT_SEQ" )
@Column(name="id")
public Long getId() {
return id;
}
This code generates below sql
create sequence RTDS_ADSINPUT_SEQ;
The problem is I wanted to specify properties like
INCREMENT BY,NOCACHE CYCLE
and the final ddl script should be some thing like below
CREATE SEQUENCE RTDS_ADSINPUT_SEQ MINVALUE 1 MAXVALUE
999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE ORDER CYCLE ;
But as far I saw hibernate only support name, sequncename,allocation,initialvalue
Please advice me if I can include those properties as annotation in the pojo.
解决方案 I looked it up in the Hibernate sources (4.2.7). It is not possible to specify this with an annotation (neither JPA nor Hibernate).
However you can provide your own Dialect to achieve this.
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
}
}
Have an entity like this
@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;
// ...
}
You can set your new Dialect as described in the Hibernate doc (http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch03.html#configuration-optional-dialects)
这篇关于使用休眠工具自动创建序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!