Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 895
Ali Avatar asked Mar 18 '14 06:03

Ali


People also ask

How do you create an object in Hibernate?

In hibernate, get() and load() are two methods which is used to fetch data for the given identifier. They both belong to Hibernate session class. Get() method return null, If no row is available in the session cache or the database for the given identifier whereas load() method throws object not found exception.

How do you update an object in Hibernate?

We can update an object in hibernate by calling the update() method, provided by the org. hibernate. Session. Though the update() method is used to update an object, there are two different ways to use update() method.

Which method is used to insert new record into database Hibernate?

Summary: save() method saves records into database by INSERT SQL query, Generates a new identifier and return the Serializable identifier back. On the other hand saveOrUpdate() method either INSERT or UPDATE based upon existence of object in database.

Which of the following is the correct method to insert object into the table in Hibernate?

Using Session#persist() and Session#save() The Session interface provides many methods to perform CRUD operations on an Entity. Its persist() method will save the entity into the database. Session session = sessionFactory.


1 Answers

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.

like image 189
Ali Avatar answered Sep 28 '22 04:09

Ali