Course和CourseCollect分别表示课程和课程收藏. 它们之间是一对多的关系, 即一个课程可以被多个人收藏. 对应的Bean类及hibernate配置文件如下:
Course类 代码
- package mypackage;
-
-
-
-
-
- public class Course implements java.io.Serializable {
-
-
-
- private String id;
-
- private String name;
-
-
-
- }
CourseCollect类 代码
- package mypackage;
-
-
-
-
-
- public class CourseCollect implements java.io.Serializable {
-
-
-
-
- private Integer collectionId;
- private Course course;
- private Integer userId;
-
-
-
- }
Course映射配置 代码
xml 代码
- <hibernate-mapping>
- <class name="mypackage.Course" table="tbl_Course">
- <id name="id" type="java.lang.String">
- <column name="CourseId" length="48" />
- <generator class="assigned">generator>
- <id>
- <property name="name" type="java.lang.String">
- <column name="Name" length="256" />
- property>
- class>
- hibernate-mapping>
CourseCollect映射配置 代码
xml 代码
- <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=?"). 但是这样处理总感觉有点怪怪的, 毕竟建了关联但是没用到.
请教各位大侠这是什么原因, 应该怎么解决(除以上两种方法外).