how to write insertinto command in hibernate criteria
Asked Answered
W

2

6

I want to write the below InsertInto query in Hibernate Criteria. Any Suggestions .. thanks for help

        sql = "insert into selectedresumes  values('" + companyId + "','"
        + resumeId + "','" + resumeStatusId + "','" + jobId + "')";
Weighting answered 5/6, 2011 at 6:42 Comment(0)
V
2

Unfortunately, You can't do it.

According to Hibernate documentation

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#batch-direct

Only the INSERT INTO ... SELECT ... form is supported; not the INSERT INTO ... VALUES ... form.

So you just need to create Object and save it using Hibernate and it should look something like that

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Resume selectedresumes  = new Resume();
//set all resume values
session.save(selectedresumes);
tx.commit();
session.close();
Vitkun answered 5/6, 2011 at 8:20 Comment(1)
thanks for your help , i am doing this same thing but at this line tx.commit(); it throws exception org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update any idea what the reason. thanksWeighting
S
0

You should map your query fileds with pojo class not with SQL or MySQL data base tables.

Like below Employee pojo object has two fields empNo, empName to insert write like below. Query query = session.createQuery("insert into Employee(empNo, empName)");

int result = query.executeUpdate();

refer this example

http://howtodoinjava.com/hibernate/hibernate-insert-query-tutorial/

Stephie answered 23/8, 2017 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.