2016-08-03 64 views
1

我是新來的休眠,並在OneToMany映射中有error Unknown column。我試圖用hibernate實現一對多的關係,但有一個錯誤。使用休眠OneToMany映射的未知列錯誤

這裏是我的代碼:

Department Class: 

@Entity 
@Table(name="department") 
public class Department { 

@Id 
@Column(name="dept_id") 
private int id; 
@Column(name="dept_name") 
private String name; 
@OneToMany(mappedBy="department", cascade=CascadeType.ALL, fetch=FetchType.EAGER) 
private Collection<Employee> employees = new ArrayList<Employee>(); 
public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public Collection<Employee> getEmployees() { 
    return employees; 
} 
public void setEmployees(Collection<Employee> employees) { 
    this.employees = employees; 
} 
} 

Employee類:

@Entity 
@Table(name = "employee") 
public class Employee { 

@Column(name="emp_id") 
@Id 
private int id; 
@Column(name="emp_name") 
private String name; 

@ManyToOne 
private Department department; 



public Department getDepartment() { 
    return department; 
} 
public void setDepartment(Department department) { 
    this.department = department; 
} 
public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
} 

員工DAO類:

public class EmployeeDao { 

public void addEmployee(Employee emp, int id){ 

    Department department = new Department(); 


    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
    Session session = sessionFactory.openSession(); 
    session.beginTransaction(); 

    department = (Department) session.get(Department.class, id); 
    emp.setDepartment(department); 
    department.getEmployees().add(emp); 


    session.save(emp); 

    session.getTransaction().commit(); 
    session.close(); 
    sessionFactory.close(); 
} 
} 

這裏是我的數據庫:

CREATE TABLE `department` (
`dept_id` int(11) NOT NULL, 
`dept_name` varchar(255) DEFAULT NULL 

CREATE TABLE `employee` (
`emp_id` int(11) NOT NULL, 
`emp_name` varchar(255) DEFAULT NULL, 
`dept_id` int(11) DEFAULT NULL 
) 

堆棧跟蹤:

WARNING: #{employee.addEmployee}: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.hibCollection.bean.Department#0] 
javax.faces.FacesException: #{employee.addEmployee}: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.hibCollection.bean.Department#0] 
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) 
at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Thread.java:722) 
Caused by: javax.faces.el.EvaluationException: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.hibCollection.bean.Department#0] 
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101) 
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
... 27 more 
Caused by: org.hibernate.exception.SQLGrammarException: could not load an entity: [com.hibCollection.bean.Department#0] 
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) 
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
at org.hibernate.loader.Loader.loadEntity(Loader.java:2041) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86) 
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:76) 
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3293) 
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:496) 
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:477) 
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:227) 
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285) 
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152) 
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090) 
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1005) 
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998) 
at com.hibCollection.dao.EmployeeDao.addEmployee(EmployeeDao.java:21) 
at com.hibCollection.mangedBean.EmployeeManageBean.addEmployee(EmployeeManageBean.java:29) 
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.apache.el.parser.AstValue.invoke(AstValue.java:278) 
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273) 
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
... 28 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'employees1_.department_dept_id' 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:406) 
at com.mysql.jdbc.Util.getInstance(Util.java:381) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543) 
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737) 
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1888) 
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) 
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953) 
at org.hibernate.loader.Loader.doQuery(Loader.java:802) 
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) 
at org.hibernate.loader.Loader.loadEntity(Loader.java:2037) 
... 49 more 

能否請你告訴我什麼,我做錯了什麼? :)

+0

你錯過員工 – Jens

+0

的多對一關係@Jens其員工類 – KOFFEE

+0

已經加入其中,是joincolumn註解? – Jens

回答

0

設法讓這麼

@ManyToOne 
@JoinColumn(name = "dept_id") 
private Department department; 
0

映射的默認列名稱類型爲 表名'_'目標表主鍵。這就是它尋找的原因department_dept_id

您可以使用@JoinColumn(name =「dept_id」)指定映射列。

+0

感謝它的工作.. – KOFFEE