-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate application without writing sql query
PostPosted: Fri Jun 13, 2008 9:18 am 
Newbie

Joined: Thu Jun 05, 2008 3:52 am
Posts: 10
Hi,

I am using EntityManager with annotation based API for my hibernate application.

To persist data i am using entityManager.persist(object).

To retrieve records and to update records, i am using select queries and update queries on bean class (schemas) according to Entitmanager documentation.

But, i want to know, is there any other way to retrieve records with using only hibernate functionality (without queries) ?

I have to retrieve records without using primary key also. I must have to use annotations based API.

So, Is there any another way to fetch and update without writing sql query.

plesase help me.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 9:38 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Hibernate offers also the Criteria API which offers an object-oriented API for query creation. This is not part of the JPA specification though. If you have to stick to the JPA specs you will have to stick with EJB3-QL.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 11:36 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
The really great thing about the Hibernate Criteria API is that it allows developers to really look at their problems in an object oriented way. You think about what you need from your object model, not how to do a query to get what you want. This is great, as Java developers are notoriously bad at SQL and HQL.

So, for example, to find all users who have verified their password, you can use the Critieria API's Example object, and just execute code such as this:

Code:
User user = new User();
    user.setVerified(false);
    Example example = Example.create(user);
    Session session = HibernateUtil.beginTransaction();
    Criteria criteria = session.createCriteria(User.class);
    criteria.add(example);
    List results = criteria.list();
    HibernateUtil.commitTransaction();
    for (int i = 0; i<results.size(); i++) {
      System.out.println(results.get(i).toString());
    }


As you can see, there is no HQL or SQL - just Java objects with various properties populated. It really is awesome.

I took this example from a little tutorial on the Hibernate3 Criteria API. If you'd like to take a look at those Hibernate examples, they're right here:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=09howtousethecriteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 2:00 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Cameron McKenzie wrote:
This is great, as Java developers are notoriously bad at SQL and HQL.


I don't know if this statement is true. But I think, if you don't really understand SQL, you won't be a good Hibernate user and you should leave this job (in real projects) to someone who has this knowledge.

It is a general misunderstanding to think that ORM tools are a way to leave SQL and the database layer behind a Java facade.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 10:43 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
It's pretty rare to be on a project that has one person that is good at everything. And Java developers are just that, Java developers.

For every problem there is a tool, and Hibernate is a good tool for Java developers to leverage their existing skills and apply it to the data layer. Nothing wrong with that!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 9:15 am 
Newbie

Joined: Thu Jun 05, 2008 3:52 am
Posts: 10
Can we use merge() method of EntityManager to update records ?

i.e.

EntityManager entityManager = null;

//
//

entityManager.merge(user);

so, is it appropriate way to use merge() method ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 10:11 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
merge

public Object merge(Object object)
throws HibernateException

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".


From the Hibernate Session JavaDoc:

http://www.hibernate.org/hib_docs/v3/api/index.html

Speaking for the Hibernate Session, loading the entities should put them into the Hibernate Session cache, so they should already be managed by the Session during the current transaction. So, if you haven't comitted the transaction after getting the objects, you shouldn't need to merge. In fact, a saveOrUpdate call would probably suffice.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.