user1217609
user1217609

Reputation: 253

java hibernate insertion

I saw a tutorial in the web showing you can do something like this for deletes and updates in Hibernate:

Query query = session.createQuery("delete Stock where stockCode = :stockCode");
query.setParameter("stockCode", "7277");
int result = query.executeUpdate();

In the same tutorial I came across this code for insertion:

Query query = session.createQuery("insert into Stock(stock_code, stock_name)" +
            "select stock_code, stock_name from backup_stock");
int result = query.executeUpdate();

My question is, is it possible to do something like the delete example from the tutorial, but for the insertion method instead, by using setParameter? I tried something like this and got an error:

Query q = session.createQuery("insert into test(:testname)");
q.setParameter("testname", "tester");

The error was regarding properties. One of my reason to use the above code is because of the

 int result =  query.executeUpdate()

which can return a result to verify whether I successfully inserted via the SQL command or not.

Upvotes: 1

Views: 6488

Answers (3)

STEFAN LARIE
STEFAN LARIE

Reputation: 35

please use EntityManager.save() insead of Query.executeUpdate()

executeUpdate returns back the number of altered rows and will return a Persistence Exception but you will not be forced to catch it and it might leat to bad exceptions

Upvotes: 0

Sinisha Mihajlovski
Sinisha Mihajlovski

Reputation: 1841

You probably have error in the sql syntax

First the SQL syntax for insert statement:

insert into <table_name> (col1, col2, col3) 
values ('1', 'second value', 'third value')

So if you have a table called "test" with only one column of type string called test_name the insert statement would be:

    insert into test (test_name) 
    values ('tester') 

Since you want the value of the test_name to be configurable you would write the code like:

Query q = session.createQuery("insert into com.yourpackage.Test (test_name) values (:testname)");
q.setParameter("testname", "tester");
int result =  query.executeUpdate()

Where com.yourpackage.Test is the POJO entity class.

On the other hand i wouldn't recommend using hibernate like this. It is better to use the save() method:

session.save(new Test("Testname"));

To verify that all is good, write an integration or JUnit test.

Have a look at this pattern:

http://salto-db.sourceforge.net/salto-db-generator/plugins/ejb3hibernatedao.html

Implement it, play around

Good luck

Upvotes: 0

Camille R
Camille R

Reputation: 1433

First thing :

setParameter

has been create for parameters in HQL queries, not field name.

Second thing :

Your request : insert into test(:testname) seems incomplete, it should be something like insert into [TABLE_NAME]([FIELD_NAME],...) values ([VALUE],...) and In that case you should use SQL queries with :

createSQLQueries

See: http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html#createSQLQuery(java.lang.String)

. Why do you need to make your query so dynamic ?

And an insert into the database would be better with a save :

session.save(youHibernateObject);

with youHibernateObject an object mapped with hibernate model mapping schemas. Good luck.

Upvotes: 1

Related Questions