Solved

Hibernate with native SQL

Posted on 2009-03-31
5
1,136 Views
Last Modified: 2013-11-24
i need to regularly update a timestamp attribute in a mysql table, while that table is not a hibernate-mapped entity class.

the SQL is something like
UPDATE mytable SET tstamp = NOW() where attr1 = val1 AND attr2 = val2

i don't want to introduce a new hibernate mapped entity just for this single statement. how can i use native SQL within a hibernate query/transaction (that updates other, hibernate-mapped tables)?
0
Comment
Question by:thomers1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Assisted Solution

by:racek
racek earned 100 total points
ID: 24036399
0
 
LVL 10

Accepted Solution

by:
mahome earned 400 total points
ID: 24036575
As far as I know you can't do updates with native SQL, only selects. You can use the connection of the session and use normal JDBC style.

Or if you have access to the dataSource you can get the connection from the datasource

Connection connection = session.connection();
Statement statement = connection.createStatement();
statement.execute("UPDATE mytable SET tstamp = NOW() where attr1 = val1 AND attr2 = val2");
 
or
 
dataSource.getConnection()
...

Open in new window

0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 24037297
      Hi!

Here is an example on how to use HQL
http://www.java2s.com/Code/Java/Hibernate/UpdateHQL.htm

Regards,
   Tomas Helgi

Ps. I posted this comment it in the other (duplicate) message by mistake.
0
 
LVL 10

Expert Comment

by:mahome
ID: 24037845
@TomasHelgi
That only works with a mapping
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 24038081
This works with or without mapping.

Regards,
   Tomas Helgi
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question