开发者

HIbernate query language problem

开发者 https://www.devze.com 2022-12-27 07:11 出处:网络
I have a Project class that has a Set of userstories called userStories12many. I\'m having troubles trying to get the project that has a certain userstory in its set getComponent(int userStoryID)

I have a Project class that has a Set of userstories called userStories12many. I'm having troubles trying to get the project that has a certain userstory in its set getComponent(int userStoryID)

I think im on the right track but i dont know what i did wrong

public Projects getComponent(int userStoryID) {
    Session session = SessionFactoryHelper.getSessionFactory()
    .getCurrentSession();
    session.beginTransaction();

    List<Projects> compo = session.createQuery("select p "
    + "from Projects as p inner join fetch p.开发者_如何学编程userStories12many as u "
    + "where u.storyId='" + userStoryID + "'").list();
    session.getTransaction().commit();
    return compo.get(0);

}


There are a few thing that feel wrong to me in your code:

  1. It's better to bind parameters rather than inserting their value directly in the query string.
  2. How come the id is quoted? isn't it an int?
  3. You are accessing the query results after the transaction is ended which is often a bad idea
0

精彩评论

暂无评论...
验证码 换一张
取 消