-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: customize save or update in hibernatetemplate
PostPosted: Thu Nov 20, 2008 8:31 am 
Newbie

Joined: Thu Nov 20, 2008 8:14 am
Posts: 6
i am using spring with hibernate and know that we can provide a string to customize the save / update. However i can hardly find any example online and i tried several times without success. can anyone provide some samples?

save(String entityName, Object entity)
Persist the given transient instance.

Say i want to update a record with contracttag as the key instead of id

update contract set description = "IBM" where contracttag = 1;

CREATE TABLE contracts
(
id integer NOT NULL ,
username char(64),
contracttag integer NOT NULL ,
.
.
)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 4:28 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
Why not get rid of SQL altogether? Hibernate provides for this.

Are you familiar with the DAO design pattern? Basically you define a class for working with a specific object type. Spring makes this pretty easy by allowing you to subclass HibernateDaoSupport. IE:

Code:
public class MyObjectDAOImpl extends HibernateDaoSupport {
   
     public void persistNewMyObject(MyObject value) {
           this.getHibernateTemplate().persist(value);
     }
}


All you need to do is inject the session factory into that object via Spring like so:
Code:
<bean id="whateverBean" class="com.persistence.MyObjectDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>


And you're ready to go. You can then create your own methods in that class relating to persistence. HibernateDaoSupport defines a bunch of simple methods for getting access to a session or session factory via getSession and getSessionFactory.

Then, use something like this in a method to update your object:
Code:
Session session = this.getSessionFactory().openSession();
Transaction trans = session.beginTransaction();
//You should probably wrap a try..catch block around the following.
MyObject object = (MyObject)session.get(MyObject.class, 1);
object.setDescription("IBM");
session.update(object);
trans.commit();
session.disconnect();
[/code]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.