com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'patient0_.doctor_idStaffMember' in 'field list'
即使我在我的数据库中确实有这个列。
StaffMember
package com.carloscortina.demo.model;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name="StaffMember")
public class StaffMember implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4402030728393694289L;
private int id;
private String firstName;
private String lastName;
private String phone;
private String cellPhone;
private String professionalNumber;
private Set<Patient> patients;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="idStaffMember")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NotNull
@Size(min=3,max=20)
@Column(name="Name")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@NotNull
@Size(min=3,max=20)
@Column(name="lastName")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name="phone")
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name="cellPhone")
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
@Column(name="ProfessionalNumber")
public String getProfessionalNumber() {
return professionalNumber;
}
public void setProfessionalNumber(String professionalNumber) {
this.professionalNumber = professionalNumber;
}
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="idPatient")
public Set<Patient> getPatients() {
return patients;
}
public void setPatients(Set<Patient> patients) {
this.patients = patients;
}
}
病人
package com.carloscortina.demo.model;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@Entity
@Table(name="Patient")
public class Patient implements Serializable{
/**
*
*/
private static final long serialVersionUID = 9026423808843575752L;
private int id;
private String firstName;
private String secondName;
private String fatherLastName;
private String motherLastName;
private String curp;
private String nickname;
private String sex;
private Date birthday;
private String notes;
private boolean active;
private StaffMember doctor;
private Timestamp addedDate;
private Set<Relative> relatives;
public Patient() {
super();
this.id = 0;
this.firstName = "";
this.secondName = "";
this.fatherLastName = "";
this.motherLastName = "";
this.curp = "";
this.nickname = "";
this.sex = "";
this.birthday = null;
this.notes = "";
this.active = false;
this.doctor = null;
this.addedDate = null;
}
public Patient(int id, String firstName, String secondName,
String fatherLastName, String motherLastName, String curp,
String nickname, String sex, Date birthday, String notes,
boolean active, StaffMember doctor, Timestamp addedDate) {
super();
this.id = id;
this.firstName = firstName;
this.secondName = secondName;
this.fatherLastName = fatherLastName;
this.motherLastName = motherLastName;
this.curp = curp;
this.nickname = nickname;
this.sex = sex;
this.birthday = birthday;
this.notes = notes;
this.active = active;
this.doctor = doctor;
this.addedDate = addedDate;
}
/**
* @return the id
*/
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="idPatient")
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the firstName
*/
@Size(min=3,max=45)
@Column(name="FirstName")
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the secondName
*/
@Size(min=3,max=45)
@Column(name="SecondName")
public String getSecondName() {
return secondName;
}
/**
* @param secondName the secondName to set
*/
public void setSecondName(String secondName) {
this.secondName = secondName;
}
/**
* @return the fatherLastName
*/
@Size(min=3,max=45)
@Column(name="FatherLastName")
public String getFatherLastName() {
return fatherLastName;
}
/**
* @param fatherLastName the fatherLastName to set
*/
public void setFatherLastName(String fatherLastName) {
this.fatherLastName = fatherLastName;
}
/**
* @return the motherLastName
*/
@Size(min=3,max=45)
@Column(name="MotherLastName")
public String getMotherLastName() {
return motherLastName;
}
/**
* @param motherLastName the motherLastName to set
*/
public void setMotherLastName(String motherLastName) {
this.motherLastName = motherLastName;
}
/**
* @return the curp
*/
@Size(min=18,max=18)
@Column(name="curp")
public String getCurp() {
return curp;
}
/**
* @param curp the curp to set
*/
public void setCurp(String curp) {
this.curp = curp;
}
/**
* @return the nickname
*/
@Size(min=1,max=45)
@Column(name="NickName")
public String getNickname() {
return nickname;
}
/**
* @param nickname the nickname to set
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* @return the sex
*/
@Size(min=8,max=9)
@Column(name="Sex")
public String getSex() {
return sex;
}
/**
* @param sex the sex to set
*/
public void setSex(String sex) {
this.sex = sex;
}
/**
* @return the birthday
*/
@NotNull
@Column(name="Birthday")
public Date getBirthday() {
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/**
* @return the notes
*/
@Column(name="Notes")
public String getNotes() {
return notes;
}
/**
* @param notes the notes to set
*/
public void setNotes(String notes) {
this.notes = notes;
}
/**
* @return the active
*/
@NotNull
@Column(name="Active")
public boolean isActive() {
return active;
}
/**
* @param active the active to set
*/
public void setActive(boolean active) {
this.active = active;
}
/**
* @return the doctor
*/
@ManyToOne
@NotFound(action=NotFoundAction.IGNORE)
public StaffMember getDoctor() {
return doctor;
}
/**
* @param doctor the doctor to set
*/
public void setDoctor(StaffMember doctor) {
this.doctor = doctor;
}
/**
* @return the addedDate
*/
public Timestamp getAddedDate() {
return addedDate;
}
/**
* @param addedDate the addedDate to set
*/
public void setAddedDate(Timestamp addedDate) {
this.addedDate = addedDate;
}
/**
* @return the relatives
*/
@OneToMany
@JoinColumn(name="idPatient")
public Set<Relative> getRelatives() {
return relatives;
}
/**
* @param relatives the relatives to set
*/
public void setRelatives(Set<Relative> relatives) {
this.relatives = relatives;
}
}
查询
@SuppressWarnings("unchecked")
@Override
public List<Patient> getPatients() {
return ( getSession().createQuery("from Patient").list());
}
表格患者
表职员
有些人能解释我的问题是什么,我该如何解决呢?
提前感谢。
错误
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/demo] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'patient0_.doctor_idStaffMember' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2040)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1837)
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:356)
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 com.carloscortina.demo.dao.HbnPatientDao.getPatients(HbnPatientDao.java:33)
at com.carloscortina.demo.service.PatientServiceImp.getPatients(PatientServiceImp.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at sun.proxy.$Proxy36.getPatients(Unknown Source)
at com.carloscortina.demo.controller.PatientsController.listAllPatients(PatientsController.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:1)
您的映射是错误的。您似乎希望Patient和StaffMember之间的一对多双向关联,由列Patient.idDoctor
映射。以下是您如何实施它:
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="idPatient")
public Set<Patient> getPatients() {
return patients;
}
...
@ManyToOne
@NotFound(action=NotFoundAction.IGNORE)
public StaffMember getDoctor() {
return doctor;
}
这会创建两个不相关的关联,因为在双向关联中,一方(在这种情况下是多方)必须具有mappedBy
属性。
所以,你创建了一个OneToMany关联,告诉Hibernate Patient中的idPatient
列是StaffMember的外键(这是错误的)。
您在Patient和StaffMember之间创建了一个不相关的ManyToOne关联,而没有告诉Hibernate使用哪个列来映射关联。因此,Hibernate使用默认列名称doctor_idStaffMember
,它不存在。
以下是必须如何映射使用idDoctor
作为外键的双向关联:
@OneToMany(mappedBy = "doctor", cascade = CascadeType.ALL)
public Set<Patient> getPatients() {
return patients;
}
...
@ManyToOne
@JoinColumn(name = "idDoctor")
public StaffMember getDoctor() {
return doctor;
}