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! :-)