一个用户表, 一个 schedule表 一对多关系
wvt_user:
userid , username .... wvt_schedule:
id , userid , ... create_day
User 类private int userid;
private String username;
private Set<Schedule> schedules = new HashSet<Schedule>();User.hbm.xml
<!-- 映射和schedules之间的关联关系 -->
<set name="schedules" inverse="true">
<key column="userid" />
<one-to-many class="Schedule" />
</set>======================
Schedule 类
private int id;private User user;private String title;<!-- 映射和User之间的关联关系 -->
<many-to-one name="user" column="userid" class="User"
not-null="true" lazy="false" />ScheduleDAOImpl:
public List<Schedule> findByDay(User user , String day, final int firstResult,
final int maxResults) {
final String hql = "from Schedule as s where s.user.userid = " + user.getUserid() +" and s.create_day ="+day;
return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session s) throws HibernateException,
SQLException {
Query query = s.createQuery(hql);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
List list = query.list();
return list;
}
});
}Test:
//要查询出 用户名ID 未2 的 并且 日期在2011-01-09 的记录 User user = new User();
user.setUserid(2); List<Schedule> list = sd.findByDay(user,"2011-01-09",0,8);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getTitle());
}Exception:
Exception in thread "main" org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [org.XXX.entity.User#2]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.XXX.entity.User#2]
Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.webvigator.entity.User#2]本人初学, 基础不太好, 请高手们指点一下 , 谢谢
wvt_user:
userid , username .... wvt_schedule:
id , userid , ... create_day
User 类private int userid;
private String username;
private Set<Schedule> schedules = new HashSet<Schedule>();User.hbm.xml
<!-- 映射和schedules之间的关联关系 -->
<set name="schedules" inverse="true">
<key column="userid" />
<one-to-many class="Schedule" />
</set>======================
Schedule 类
private int id;private User user;private String title;<!-- 映射和User之间的关联关系 -->
<many-to-one name="user" column="userid" class="User"
not-null="true" lazy="false" />ScheduleDAOImpl:
public List<Schedule> findByDay(User user , String day, final int firstResult,
final int maxResults) {
final String hql = "from Schedule as s where s.user.userid = " + user.getUserid() +" and s.create_day ="+day;
return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session s) throws HibernateException,
SQLException {
Query query = s.createQuery(hql);
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
List list = query.list();
return list;
}
});
}Test:
//要查询出 用户名ID 未2 的 并且 日期在2011-01-09 的记录 User user = new User();
user.setUserid(2); List<Schedule> list = sd.findByDay(user,"2011-01-09",0,8);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getTitle());
}Exception:
Exception in thread "main" org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException: No row with the given identifier exists: [org.XXX.entity.User#2]; nested exception is org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.XXX.entity.User#2]
Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.webvigator.entity.User#2]本人初学, 基础不太好, 请高手们指点一下 , 谢谢
HQL的日期格式 可能和数据库的不一样 并且你的前天显示
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getTitle());
}
也没做非空判断
<many-to-one name="user" column="userid" class="User"
not-null="true" lazy="false" />
你的class指定的User路径不全,导致找不到。所以会报not-found 异常。
你可以将<many-to-one>的not-found="ignore";
not-found:指定的外键引用的数据不存在时如何处理。
看看去
配置还可以