我有Java类CourseOffered
CourseTaker
和Teacher
,其中CourseTaker
具有Teacher
和CourseOffered
的主键作为外键。CourseOffered
类的外观如下((为了使内容更小,我删除了一些基本的constructors
和Getters
和Setters
;)
package subHibernate.entity;
@Entity
@Table(name = "course_offered")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "CourseOffered.findAll", query = "SELECT c FROM CourseOffered c"),
@NamedQuery(name = "CourseOffered.findByCourseId", query = "SELECT c FROM CourseOffered c WHERE c.courseId = :courseId"),
@NamedQuery(name = "CourseOffered.findByCourseCode", query = "SELECT c FROM CourseOffered c WHERE c.courseCode = :courseCode"),
@NamedQuery(name = "CourseOffered.findByCourseTitle", query = "SELECT c FROM CourseOffered c WHERE c.courseTitle = :courseTitle"),
@NamedQuery(name = "CourseOffered.findBySession", query = "SELECT c FROM CourseOffered c WHERE c.session = :session"),
@NamedQuery(name = "CourseOffered.findBySemester", query = "SELECT c FROM CourseOffered c WHERE c.semester = :semester")})
public class CourseOffered implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "course_id")
private Integer courseId;
@Basic(optional = false)
@Column(name = "course_code")
private String courseCode;
@Basic(optional = false)
@Column(name = "course_title")
private String courseTitle;
@Basic(optional = false)
@Column(name = "session")
private String session;
@Basic(optional = false)
@Column(name = "semester")
private String semester;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "caCourseId")
private List<CourseTaker> courseTakerList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "crCourseId")
private List<CourseRegistration> courseRegistrationList;
public CourseOffered() {
}
//Some Constructors
// Getters and Setters which I deliberately Ignored as they are not
// Important here
@XmlTransient
public List<CourseTaker> getCourseTakerList() {
return courseTakerList;
}
public void setCourseTakerList(List<CourseTaker> courseTakerList) {
this.courseTakerList = courseTakerList;
}
@XmlTransient
public List<CourseRegistration> getCourseRegistrationList() {
return courseRegistrationList;
}
public void setCourseRegistrationList(List<CourseRegistration> courseRegistrationList) {
this.courseRegistrationList = courseRegistrationList;
}
public java.lang.Integer getPrimaryKey(){
return getCourseId();
}
}
Teacher
类看起来像这样,@Entity
@Table(name = "teacher")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Teacher.findAll", query = "SELECT t FROM Teacher t"),
@NamedQuery(name = "Teacher.findByTeacherId", query = "SELECT t FROM Teacher t WHERE t.teacherId = :teacherId"),
@NamedQuery(name = "Teacher.findByTeaCode", query = "SELECT t FROM Teacher t WHERE t.teaCode = :teaCode"),
@NamedQuery(name = "Teacher.findByTeaPassword", query = "SELECT t FROM Teacher t WHERE t.teaPassword = :teaPassword"),
@NamedQuery(name = "Teacher.findByTeaName", query = "SELECT t FROM Teacher t WHERE t.teaName = :teaName"),
@NamedQuery(name = "Teacher.findByTeaEmail", query = "SELECT t FROM Teacher t WHERE t.teaEmail = :teaEmail"),
@NamedQuery(name = "Teacher.findByTeaDegignation", query = "SELECT t FROM Teacher t WHERE t.teaDegignation = :teaDegignation")})
public class Teacher implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "teacher_id")
private Integer teacherId;
@Basic(optional = false)
@Column(name = "tea_code")
private String teaCode;
@Basic(optional = false)
@Column(name = "tea_password")
private String teaPassword;
@Basic(optional = false)
@Column(name = "tea_name")
private String teaName;
@Basic(optional = false)
@Column(name = "tea_email")
private String teaEmail;
@Basic(optional = false)
@Column(name = "tea_degignation")
private String teaDegignation;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "caTeacherId")
private List<CourseTaker> courseTakerList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tgTeacherId")
private List<TeacherGroup> teacherGroupList;
public Teacher() {
}
// Some Constructors
// Getters And Setters Ignored
@XmlTransient
public List<CourseTaker> getCourseTakerList() {
return courseTakerList;
}
public void setCourseTakerList(List<CourseTaker> courseTakerList) {
this.courseTakerList = courseTakerList;
}
@XmlTransient
public List<TeacherGroup> getTeacherGroupList() {
return teacherGroupList;
}
public void setTeacherGroupList(List<TeacherGroup> teacherGroupList) {
this.teacherGroupList = teacherGroupList;
}
}
最后是
CourseTaker
类@Entity
@Table(name = "course_taker")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "CourseTaker.findAll", query = "SELECT c FROM CourseTaker c"),
@NamedQuery(name = "CourseTaker.findByCourseTakerId", query = "SELECT c FROM CourseTaker c WHERE c.courseTakerId = :courseTakerId")})
public class CourseTaker implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "course_taker_id")
private Integer courseTakerId;
@JoinColumn(name = "ca_course_id", referencedColumnName = "course_id")
@ManyToOne(optional = false)
private CourseOffered caCourseId;
@JoinColumn(name = "ca_teacher_id", referencedColumnName = "teacher_id")
@ManyToOne(optional = false)
private Teacher caTeacherId;
public CourseTaker() {
}
public CourseTaker(Integer courseTakerId) {
this.courseTakerId = courseTakerId;
}
public Integer getCourseTakerId() {
return courseTakerId;
}
public void setCourseTakerId(Integer courseTakerId) {
this.courseTakerId = courseTakerId;
}
public CourseOffered getCaCourseId() {
return caCourseId;
}
public void setCaCourseId(CourseOffered caCourseId) {
this.caCourseId = caCourseId;
}
public Teacher getCaTeacherId() {
return caTeacherId;
}
public void setCaTeacherId(Teacher caTeacherId) {
this.caTeacherId = caTeacherId;
}
}
我也有一个
CourseTakerService
类,看起来像这样,public class CourseTakerService implements ICourseTakerService , LocatableService {
private static Logger log = LogService.getLogger(CourseTakerService.class);
public void init() {
}
public void destroy() {
}
/**
* Adds a new courseTaker to the database.
*
* @param model a data object
* @return CourseTaker a data object with the primary key
*/
public subHibernate.entity.CourseTaker addCourseTaker(subHibernate.entity.CourseTaker model) throws GenericBusinessException {
subHibernate.HibernateQueryHelper hibernateTemplate = new subHibernate.HibernateQueryHelper();
try {
hibernateTemplate.save(model);
return getCourseTaker(model.getPrimaryKey());
} finally {
log.debug("finished addCourseTaker(subHibernate.entity.CourseTaker model)");
}
/**
*
* Retrieves a list of data object for the specified caTeacherId field. To
* use a wildcard search, use a % in the query.
*
* @param caTeacherId the field
* @return List of CourseTaker data objects, empty list in case no results
* were found.
*/
public java.util.List findCourseTakerByCaTeacherId(java.lang.Integer caTeacherId) throws GenericBusinessException {
subHibernate.HibernateQueryHelper hibernateTemplate = new subHibernate.HibernateQueryHelper();
try {
String queryString = "from " + CourseTaker.class.getName() + " e where e.caTeacherId like :caTeacherId ";
// Add a an order by on all primary keys to assure reproducable results.
String orderByPart = "";
orderByPart += " order by e.courseTakerId";
queryString += orderByPart;
Query query = hibernateTemplate.createQuery(queryString);
hibernateTemplate.setQueryParameter(query, "caTeacherId", caTeacherId);
List list = hibernateTemplate.list(query);
return list;
} finally {
log.debug("finished findCourseTakerByCaTeacherId(java.lang.Integer caTeacherId)");
}
}
}
当我打电话给
list = (List) courseTakerService.findCourseTakerByCaTeacherId(1);
它产生以下堆栈跟踪
[保证courseTaker表中的一行具有courseTeacherId = 1]
27-Feb-2015 11:11:31.539 ERROR [http-nio-8084-exec-32] com.finalist.util.log.JDKLogger.error Error while getting the hibernate query list.
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of subHibernate.entity.Teacher.teacherId
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:62)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:344)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4537)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4259)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:209)
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:248)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:510)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:66)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:612)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1875)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
at org.hibernate.loader.Loader.doQuery(Loader.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at subHibernate.HibernateQueryHelper.list(HibernateQueryHelper.java:94)
at subHibernate.session.CourseTakerService.findCourseTakerByCaTeacherId(CourseTakerService.java:270)
at OffiAssignServlet.processRequest(OffiAssignServlet.java:116)
at OffiAssignServlet.doPost(OffiAssignServlet.java:174)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field subHibernate.entity.Teacher.teacherId to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:387)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:59)
... 53 more
subHibernate.exception.GenericBusinessException: Error while getting the hibernate query list.
at subHibernate.HibernateQueryHelper.list(HibernateQueryHelper.java:98)
at subHibernate.session.CourseTakerService.findCourseTakerByCaTeacherId(CourseTakerService.java:270)
at OffiAssignServlet.processRequest(OffiAssignServlet.java:116)
at OffiAssignServlet.doPost(OffiAssignServlet.java:174)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of subHibernate.entity.Teacher.teacherId
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:62)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:344)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4537)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4259)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:209)
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:248)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:510)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:174)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:66)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:612)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1875)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1836)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
at org.hibernate.loader.Loader.doQuery(Loader.java:900)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
at org.hibernate.loader.Loader.doList(Loader.java:2526)
at org.hibernate.loader.Loader.doList(Loader.java:2512)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2342)
at org.hibernate.loader.Loader.list(Loader.java:2337)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1269)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at subHibernate.HibernateQueryHelper.list(HibernateQueryHelper.java:94)
... 29 more
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Integer field subHibernate.entity.Teacher.teacherId to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.lang.reflect.Field.get(Field.java:387)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:59)
... 53 more
最佳答案
我认为您的查询字符串需要按如下所示进行更改。请尝试使用此方法。
e.caTeacherId将引用Teacher对象,并且将其与TeacherId值进行比较。这可能是问题所在。
String queryString = "from " + CourseTaker.class.getName() + " e where e.caTeacherId.teacherId like :caTeacherId ";