[hibernate - jpa ] good practices and bad practices
- by blow
Hi all, i have some questions about interaction with hibernate.
openSession or getCurrentSession (without jta, thread insted)?
How mix session operations with swing gui? Is good have something like this in a javabean class?
public void actionPerformed(ActionEvent event) {
// session code
}
Can i add methods to my entities that contains hql queries or is a bad practice?
For example:
// This method is in an entity MyOtherEntity.java class
public int getDuration(){
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
int sum=(Integer)session.createQuery("select sum(e.duration) as duration from MyEntity as e where e.myOtherEntity.id=:id group by e.name").
.setLong("id", getId());
.uniqueResult();
return sum;
}
In alternative how can i do this in a better and elegant way?
Thanks.