请问一个配置一对多关系的有关问题
请教一个配置一对多关系的问题Course和CourseCollect分别表示课程和课程收藏. 它们之间是一对多的关系, 即
请教一个配置一对多关系的问题
Course和CourseCollect分别表示课程和课程收藏. 它们之间是一对多的关系, 即一个课程可以被多个人收藏. 对应的Bean类及hibernate配置文件如下:
- <hibernate-mapping>??? ??
- ????<class?name="mypackage.CourseCollect"?table="tbl_UserCollection_Course"?>??? ??
- ????????<id?name="collectionId"?type="java.lang.Integer">??? ??
- ????????????<column?name="CollectionId"?/>??? ??
- ????????????<generator?class="native">generator>??? ??
- ????????id>??? ??
- ????????<property?name="userId"?type="java.lang.Integer">??? ??
- ????????????<column?name="UserId"?/>??? ??
- ????????property>??? ??
- ????????<many-to-one?name="course"?class="mypackage.Course"?fetch="select">??? ??
- ????????????<column?name="Course"?length="48"?/>??? ??
- ????????many-to-one>??? ??
- ????class>??? ??
- hibernate-mapping>???
这样的配置在执行以下这个调用时出现了异常:
java 代码?
- private?static?final??String?BOOL_COURSE_ISCOLLECT?=?"from?CourseCollect?where?userId=??and?course=?";??
- ?
//通过userId和课程调用查找方法,以判断用户是否已收藏某门课程?? - find(BOOL_COURSE_ISCOLLECT,new?Object[]{userId,courseId});
??? 异常信息:?
- 严重:?IllegalArgumentException?in?class: mypackage.Course,?getter?method?of?property:?id??
- org.springframework.orm.hibernate3.HibernateSystemException:
- IllegalArgumentException?occurred?calling?getter?of?mypackage.Course.id;
- nested?exception?is?org.hibernate.PropertyAccessException:
- IllegalArgumentException?occurred?calling?getter?of?mypackage.Course.id??
- Caused?by:?org.hibernate.PropertyAccessException:
- IllegalArgumentException?occurred?calling?getter?of?mypackage.Course.id??
- ????at?org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)??
- ????at?org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)??
- ????at?org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3591)??
- ????at?org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3307)??
- ????at?org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)??
- ????at?org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:218)??
- ????at?org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)??
- ????at?org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:87)??
- ????at?org.hibernate.param.PositionalParameterSpecification.bind(PositionalParameterSpecification.java:39)??
- ????at?org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:67)??
- ????at?org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:396)??
- ????at?org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:259)??
- ????at?org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1141)??
- ????at?org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)??
- ????at?org.springframework.orm.hibernate3.HibernateTemplate$38.doInHibernate(HibernateTemplate.java:1063)??
- ????at?org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)??
- ????at?org.springframework.orm.hibernate3.HibernateTemplate.bulkUpdate(HibernateTemplate.java:1054)??
如果把其中Course中的id改为int类型的话是没有问题的. 另外如果把运行的HQL语句改为:
改过的HQL语句
?
- private?static?final??String?BOOL_COURSE_ISCOLLECT?=?"from?CourseCollect?where?userId=??and?course.id=?";??
运行也没有问题(即将"course=?"这个条件改为"course.id=?"). 但是这样处理总感觉有点怪怪的, 毕竟建了关联但是没用到.
??
请教各位大侠这是什么原因, 应该怎么解决(除以上两种方法外). 1 楼 hunteagle 2007-08-07 怎么显示的XML代码这么乱, 多了好多<!--</span-->这个东西. 不知道大家看不看得清... 2 楼 jinlibing 2007-08-07 http://www.java2s.com/Code/Java/Hibernate/Relation-One-to-Many.htm
http://www.java2s.com/Code/Java/Hibernate/Relation-Many-to-Many.htm