简体   繁体   中英

How to insert new items with Hibernate?

I have an entity class called Task which is mapped in hibernate.

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$1.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.

What am I doing wrong?

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

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

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

@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.

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.

I accidentally removed hibernate_sequence table in my db. Change:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM