Solved

Hibernate with native SQL

Posted on 2009-03-31
5
1,135 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

726 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