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:
- It's better to bind parameters rather than inserting their value directly in the query string.
- How come the id is quoted? isn't it an int?
- You are accessing the query results after the transaction is ended which is often a bad idea
精彩评论