Java 如何使用 Hibernate 插入新项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22472292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:57:22  来源:igfitidea点击:

How to insert new items with Hibernate?

javamysqlhibernate

提问by Click Upvote

I have an entity class called Taskwhich is mapped in hibernate.

我有一个名为的实体类Task,它在休眠中映射。

I'm able to fetch existing items belonging to this class correctly, so I don't think this is an issue with the mapping of the class.

我能够正确获取属于此类的现有项目,因此我认为这不是类映射的问题。

However, when I try to insert a new item, I get the following error:

但是,当我尝试插入新项目时,出现以下错误:

org.hibernate.exception.SQLGrammarException: error performing isolated work
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:96)
    at org.hibernate.id.enhanced.TableStructure.getNextValue(TableStructure.java:131)
    at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:58)
    at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:422)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:680)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:672)

In the following code:

在以下代码中:

Task t = new Task();
t.title = "foo";
t.comment = "bar";
//snip... (every single field of task is given some value, except for id)

session.saveOrUpdate("Task", t); //This is the line on which the exception occurs

If I don't do saveOrUpdate(), then the new task isn't inserted into the db.

如果我不这样做saveOrUpdate(),则新任务不会插入到数据库中。

What am I doing wrong?

我究竟做错了什么?

Edit: session.save(task)doesn't work either

编辑:session.save(task)也不起作用

采纳答案by Click Upvote

Got this to work with the help of this link: http://www.coderanch.com/t/487173/ORM/databases/hibernate-sequence-exist

在此链接的帮助下得到它:http: //www.coderanch.com/t/487173/ORM/databases/hibernate-sequence-exist

Apparently hibernate looks for sequence tables for generating the id. Setting the following:

显然 hibernate 寻找序列表来生成 id。设置以下内容:

@GeneratedValue(strategy = GenerationType.IDENTITY)

on the id, causes it to use the underlying db's auto increment and not try to generate the id itself, and now it works.

在 id 上,导致它使用底层数据库的自动增量而不是尝试生成 id 本身,现在它可以工作了。

回答by numsu

If someone else is struggling with this error, I fixed it by removing

如果其他人正在为此错误而苦苦挣扎,我通过删除来修复它

<prop key="hibernate.id.new_generator_mappings">true</prop>

From hibernate properties.

从休眠属性。

回答by lord5et

I accidentally removed hibernate_sequence table in my db. Change:

我不小心删除了数据库中的 hibernate_sequence 表。改变:

spring.jpa.hibernate.ddl-auto=none

to

spring.jpa.hibernate.ddl-auto=create

In your application.properties file so Hibernate could recreate this table by himself

在您的 application.properties 文件中,以便 Hibernate 可以自己重新创建此表