Solved

Hibernate with native SQL

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

828 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