-->
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.  [ 1 post ] 
Author Message
 Post subject: Queries and Join (Parameter, Efficiency)
PostPosted: Wed Feb 02, 2011 5:11 am 
Newbie

Joined: Mon Jan 24, 2011 1:38 pm
Posts: 8
Hello!
I've got two questions to my hibernate queries and a hibernate join:

1.
Actually my queries are looking like this:

Code:
    // querying with primary key
    public ClassA getClassAById(int id) {
        return (ClassA) this.sessionFactory.getCurrentSession().get(ClassA.class, id);
    }

    // querying without primary key
    public ClassA getClassAByName(String name) {
        ClassA a = (ClassA) this.sessionFactory.getCurrentSession().createQuery("from ClassA classa where classa.name=?").setParameter(0, name).uniqueResult();
        return a;
    }


This works fine, but I would like to know if the second query is a recommended way to query. Would it be better not to use the "?", but a ":paramentername"?
Like:
Code:
    public ClassA getClassAByName(String name) {
        ClassA a = (ClassA) this.sessionFactory.getCurrentSession().createQuery("from ClassA classa where classa.name=:name").setParameter("name", name).uniqueResult();
        return a;
    }


Where are the advantages/disadvantages or is there something more preferred?

2.
My second question is about a Join. Its the first Join I wrote in Hibernate.
The goal: Ive got two tables: Users(users_id, username, ...) and Users_information(users_id, email, ...)
Before querying I got the Users-Email, but I want to know the Users username.

My first approach, without Join:

Code:
// projectDao is my HibernateDao-Class, containing the querying-Methods

   String username = projectDao.getUsersInformationByEmail(email).getUsers().getUsername();

This works, but I would like to know what is more efficient. To query the Users_Information-Object by the email-adress and then call getUsers().getUsername() (requesting the depending data) or to use a join.


Code:
    public String getUsernameByEmail(String email) {
        Users u = (Users) this.sessionFactory.getCurrentSession().createQuery("from Users,UsersInformation users where usersinformation.usersId=users.usersId and usersinformation.email=:email").setParameter("email", email).uniqueResult();   
        return u.getUsername();
    }


Is there one approach which is more efficient? And if not, beside of this, could you tell me if my Join is correct? :-)

Thank you for helping me! :-)


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

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.