Java org.hibernate.dialect.OracleDialect 不支持身份密钥生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24009042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.dialect.OracleDialect does not support identity key generation
提问by being_uncertain
I was trying to import a sample project in to eclipse and was facing the below given error up on running the application.
我试图将一个示例项目导入到 eclipse 中,并且在运行应用程序时遇到了下面给出的错误。
Caused by: org.hibernate.MappingException: org.hibernate.dialect.OracleDialect does not support identity key generation
at org.hibernate.dialect.Dialect.getIdentityColumnString(Dialect.java:743)
at org.hibernate.dialect.Dialect.getIdentityColumnString(Dialect.java:733)
at org.hibernate.mapping.Table.sqlCreateString(Table.java:426)
at org.hibernate.cfg.Configuration.generateSchemaCreationScript(Configuration.java:1028)
at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:125)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:492)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:247)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:373)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:358)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 32 more
As per thisSO link, I have changed the
根据此SO 链接,我已更改
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.IDENTITY)
to
到
@GeneratedValue(strategy = GenerationType.AUTO)
or @GeneratedValue(strategy = GenerationType.TABLE)
@GeneratedValue(strategy = GenerationType.AUTO)
或者 @GeneratedValue(strategy = GenerationType.TABLE)
But didn't work.
但是没有用。
Here is the code:
这是代码:
User.java:
用户.java:
@Entity
@Table(name = "users")
@ManagedBean
@ViewScoped
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "role", nullable = false)
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
From the applicationContext.xml:
从 applicationContext.xml:
<!-- Session Factory Declaration -->
<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.crud.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
采纳答案by JamesENL
You can use tell Hibernate to use a sequence to generate your ID's
您可以使用告诉 Hibernate 使用序列来生成您的 ID
@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")
private int id;
This config basically tells Hibernate to use a database sequence called ID_SEQ to generate the ID's for this object. You can specify other sequences on other objects if you want other unique ID's or you can use the same sequence if you want globally unique ID's across your entire system.
这个配置基本上告诉 Hibernate 使用一个名为 ID_SEQ 的数据库序列来生成这个对象的 ID。如果您想要其他唯一 ID,您可以在其他对象上指定其他序列,或者如果您想要整个系统中的全局唯一 ID,您可以使用相同的序列。
The only downside to this is that can't perform batch inserts (without some further config) because Hibernate needs to get the next sequence value from the database every time, and you can't use this config if you want to use a MySQL database, because they don't support sequences.
唯一的缺点是不能执行批量插入(没有一些进一步的配置),因为 Hibernate 每次都需要从数据库中获取下一个序列值,如果你想使用 MySQL 数据库,你不能使用这个配置,因为它们不支持序列。
If any of that doesn't make sense let me know and I'll explain it further.
如果其中任何一个没有意义,请告诉我,我会进一步解释。
回答by Himanshu Tyagi
回答by iamharish15
You can just use @GeneratedValue(strategy = GenerationType.TABLE)
if you just need to be able to auto increment the value for attributes such as some ID which is primary key of your table. It worked for me. Hope it helps.
你可以使用@GeneratedValue(strategy = GenerationType.TABLE)
,如果你只需要能够自动递增的属性值如一些ID是你的表的主键。它对我有用。希望能帮助到你。
回答by Jordi M.
my reputation is too low...
我的名气太低了...
Well, I'm very grateful to JamesENL
嗯,我非常感谢 JamesENL
I substituted
我替换了
@GeneratedValue(strategy = GenerationType.IDENTITY)
by
经过
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")`
and that works fine
这工作正常
回答by lupchiazoem
The reason for exception is:
异常的原因是:
Hibernate expects from underlying database to provide an auto increment feature for a given property, in your case it's id
. IOW, Oracle(your case) should support auto increment feature for a field. Oracle started to provide auto increment feature with 12c versionand, as your version was less, you got that exception .
Hibernate 期望底层数据库为给定的属性提供自动增量功能,在您的情况下它是id
. IOW,Oracle(您的情况)应该支持字段的自动增量功能。Oracle 开始在12c 版本中提供自动增量功能,并且由于您的版本较少,您会遇到该异常。
回答by Siddharth Gupta
You can change .identity
by .sequence
and it will work:
你可以改变.identity
的.sequence
,它会工作:
GeneratedValue(strategy=GenerationType.SEQUENCE)
回答by Kuriozo George
I also had the same problem I am using OracleDB, I tried Identity, GenerateSequence and nothing gave me the solution. Until I had to change the dialect in my application properties. For some reason the dialect was not generating the correct sequence, which is why I decided to use a different dialect for my Oracle12c case.
我在使用 OracleDB 时也遇到了同样的问题,我尝试了 Identity、GenerateSequence,但没有给我解决方案。直到我不得不更改应用程序属性中的方言。由于某种原因,方言没有生成正确的序列,这就是为什么我决定为我的 Oracle12c 案例使用不同的方言。
Before:
前:
spring.jpa.database-platform = org.hibernate.dialect.Oracle10gDialect
Then:
然后:
spring.jpa.database-platform = org.hibernate.dialect.Oracle12cDialect
You could verify that you have value in the dialect of your connection maybe and changing it to a different version will be resolved like me.
您可以验证您的连接方言是否具有价值,并将其更改为其他版本将像我一样解决。