Hibernate:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列 [英] Hibernate: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column in 'field list'
问题描述
我想在不创建额外表的情况下实现OneToMany / ManyToOne映射。
我有两个实体:
(1)Employee.java
@Entity(name =EMPLOYEE_DETAILS)
public class Employee {
private int empId;
私人字符串employeeName;
私人日期joiningDate;
私人收藏< Project> project = new ArrayList< Project>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getEmpId(){
return empId;
}
public void setEmpId(int employeeId){
this.empId = employeeId;
}
public String getEmployeeName(){
return employeeName;
}
public void setEmployeeName(String employeeName){
this.employeeName = employeeName;
@Temporal(TemporalType.DATE)
public Date getJoiningDate(){
return joiningDate;
}
public void setJoiningDate(Date joiningDate){
this.joiningDate = joiningDate;
}
@OneToMany(mappedBy =employeeObj)
public Collection< Project> getProject(){
return project;
}
public void setProject(Collection< Project> project){
this.project = project;
$ b (2)Project.java
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int projectId;
private String projectName;
@ManyToOne
@JoinColumn(name =EMPLOYEE_ID)
私人员工employeeObj;
public int getProjectId(){
return projectId;
}
public void setProjectId(int projectId){
this.projectId = projectId;
}
public String getProjectName(){
return projectName;
}
public void setProjectName(String projectName){
this.projectName = projectName;
}
public Employee getEmployeeObj(){
return employeeObj;
}
public void setEmployeeObj(Employee employeeObj){
this.employeeObj = employeeObj;
}
}
我有一个主类:
$ b $(3)MainClass.java
public class MainClass {
public static void main(String [] args){
Collection< Project> collection = new ArrayList< Project>();
Project projectA = new Project();
projectA.setProjectName(ABC);
项目projectB = new Project();
projectB.setProjectName(DEF);
collection.add(projectA);
collection.add(projectB);
Employee employee = new Employee();
employee.setEmployeeName(Swarup Saha);
employee.setJoiningDate(new Date());
employee.setProject(collection);
//多对一配置
projectA.setEmployeeObj(employee);
projectB.setEmployeeObj(employee);
Configuration configuration = new Configuration()。configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()。
applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());
会话会话= factory.openSession();
session.beginTransaction();
session.save(employee);
session.save(projectA);
session.save(projectB);
session.getTransaction()。commit();
session.close();
factory.close();
$ / code>
当我运行这个时,控制台。
错误:表'project'已存在
Hibernate:alter table项目添加约束FK_aj4dhrm7m2sit4f8tcimf1121外键(EMPLOYEE_ID)引用EMPLOYEE_DETAILS(empId)
2015年3月25日上午12时39分29秒org.hibernate.tool.hbm2ddl.SchemaExport执行
错误:HHH000389:不成功:alter table项目添加约束FK_aj4dhrm7m2sit4f8tcimf1121外键(EMPLOYEE_ID)引用EMPLOYEE_DETAILS(empId)
2015年3月25日上午12时39分29秒org.hibernate.tool.hbm2ddl.SchemaExport执行
错误:键列'EMPLOYEE_ID'在表中不存在
Mar 25,2015 12:39:29 AM org.hibernate.tool.hbm2ddl.SchemaExport执行
INFO:HHH000230:模式导出完成
Hibernate:插入到EMPLOYEE_DETAILS(employeeName,joiningDate)值(?,?)
Hibernate:插入Project(EMPLOYEE_ID,projectName)的值(?,?)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
警告: SQL错误:1054,SQLState:42S22
2015年3月25日上午12时39分30秒org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
错误:'字段列表'中的未知列'EMPLOYEE_ID'
2015年3月25日上午12时39分30秒org.hibernate.engine.jdbc.spi.SqlExceptionHelper $ StandardWarningHandler logWarning
警告:SQL警告代码:1054,SQLState:42S22
Mar 25, 2015 12:39:30 org.hibernate.engine.jdbc.spi.SqlExceptionHelper $ StandardWarningHandler logWarning
WARN:'字段列表'中的未知列'EMPLOYEE_ID'
由com.mysql.jdbc引起。 exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中未知列'EMPLOYEE_ID'
任何建议有什么问题我在这里做了什么?
解决方案代码中没有问题。我删除了数据库中的所有表,然后运行MainClass,程序成功运行。 :D
Hibernate:alter table Project drop foreign key FK_p6ig2lkv4igor3itfy6v11sy4
2015年3月25日11:35:09 PM org .hibernate.tool.hbm2ddl.SchemaExport执行
错误:HHH000389:不成功:alter table项目放置外键FK_p6ig2lkv4igor3itfy6v11sy4
2015年3月25日下午11:35:09 org.hibernate.tool.hbm2ddl.SchemaExport执行
错误:表'hibernatedb.project'不存在
Hibernate:删除表如果存在EMPLOYEE_DETAILS
Hibernate:删除表如果存在项目
Hibernate:创建表EMPLOYEE_DETAILS(EMP_ID integer不是null auto_increment,employeeName varchar(255),joiningDate日期,主键(EMP_ID))
Hibernate:create table Project(projectId integer not null auto_increment,projectName varchar(255),EMP_ID integer not null,primary key(projectId ))
Hibernate:alter table项目添加约束FK_p6ig2lkv4igor3itfy6v11sy4外键(EMP_ID)引用EMPLOYEE_DETAILS(EMP_ID)
Mar 25, 2015 11:35:09 PM org.hibernate.tool.hbm2ddl.SchemaExport执行
INFO:HHH000230:模式导出完成
Hibernate:插入到EMPLOYEE_DETAILS(employeeName,joiningDate)values(?,?)
Hibernate:插入Project(EMP_ID,projectName)值(?,?)
Hibernate:插入Project(EMP_ID,projectName)值(?,?)
2015年3月25日11:35: 09 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO:HHH000030:清理连接池[jdbc:mysql:// localhost / hibernateDB]
I am getting this issue and not able to resolve.
I want to implement OneToMany/ManyToOne mapping without creating extra table.
I have two Entity:
(1) Employee.java
@Entity(name="EMPLOYEE_DETAILS")
public class Employee {
private int empId;
private String employeeName;
private Date joiningDate;
private Collection<Project> project = new ArrayList<Project>();
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
public int getEmpId() {
return empId;
}
public void setEmpId(int employeeId) {
this.empId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
@Temporal (TemporalType.DATE)
public Date getJoiningDate() {
return joiningDate;
}
public void setJoiningDate(Date joiningDate) {
this.joiningDate = joiningDate;
}
@OneToMany(mappedBy="employeeObj")
public Collection<Project> getProject() {
return project;
}
public void setProject(Collection<Project> project) {
this.project = project;
}
}
(2) Project.java
@Entity
public class Project {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private int projectId;
private String projectName;
@ManyToOne
@JoinColumn(name="EMPLOYEE_ID")
private Employee employeeObj;
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public Employee getEmployeeObj() {
return employeeObj;
}
public void setEmployeeObj(Employee employeeObj) {
this.employeeObj = employeeObj;
}
}
And I Have a main class:
(3) MainClass.java
public class MainClass {
public static void main(String[] args) {
Collection<Project> collection = new ArrayList<Project>();
Project projectA = new Project();
projectA.setProjectName("ABC");
Project projectB = new Project();
projectB.setProjectName("DEF");
collection.add(projectA);
collection.add(projectB);
Employee employee = new Employee();
employee.setEmployeeName("Swarup Saha");
employee.setJoiningDate(new Date());
employee.setProject(collection);
//Many To One Configuration
projectA.setEmployeeObj(employee);
projectB.setEmployeeObj(employee);
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());
Session session = factory.openSession();
session.beginTransaction();
session.save(employee);
session.save(projectA);
session.save(projectB);
session.getTransaction().commit();
session.close();
factory.close();
}
}
When I am running this I am getting this issue in the console.
ERROR: Table 'project' already exists
Hibernate: alter table Project add constraint FK_aj4dhrm7m2sit4f8tcimf1121 foreign key (EMPLOYEE_ID) references EMPLOYEE_DETAILS (empId)
Mar 25, 2015 12:39:29 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Project add constraint FK_aj4dhrm7m2sit4f8tcimf1121 foreign key (EMPLOYEE_ID) references EMPLOYEE_DETAILS (empId)
Mar 25, 2015 12:39:29 AM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Key column 'EMPLOYEE_ID' doesn't exist in table
Mar 25, 2015 12:39:29 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into EMPLOYEE_DETAILS (employeeName, joiningDate) values (?, ?)
Hibernate: insert into Project (EMPLOYEE_ID, projectName) values (?, ?)
Mar 25, 2015 12:39:30 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1054, SQLState: 42S22
Mar 25, 2015 12:39:30 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'EMPLOYEE_ID' in 'field list'
Mar 25, 2015 12:39:30 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 1054, SQLState: 42S22
Mar 25, 2015 12:39:30 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: Unknown column 'EMPLOYEE_ID' in 'field list'
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'EMPLOYEE_ID' in 'field list'
Any suggestion what's wrong I am doing over here?
解决方案 There is no problem in the code. I deleted all the tables from database and then run the MainClass and the program run successfully. :D
Hibernate: alter table Project drop foreign key FK_p6ig2lkv4igor3itfy6v11sy4
Mar 25, 2015 11:35:09 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: HHH000389: Unsuccessful: alter table Project drop foreign key FK_p6ig2lkv4igor3itfy6v11sy4
Mar 25, 2015 11:35:09 PM org.hibernate.tool.hbm2ddl.SchemaExport perform
ERROR: Table 'hibernatedb.project' doesn't exist
Hibernate: drop table if exists EMPLOYEE_DETAILS
Hibernate: drop table if exists Project
Hibernate: create table EMPLOYEE_DETAILS (EMP_ID integer not null auto_increment, employeeName varchar(255), joiningDate date, primary key (EMP_ID))
Hibernate: create table Project (projectId integer not null auto_increment, projectName varchar(255), EMP_ID integer not null, primary key (projectId))
Hibernate: alter table Project add constraint FK_p6ig2lkv4igor3itfy6v11sy4 foreign key (EMP_ID) references EMPLOYEE_DETAILS (EMP_ID)
Mar 25, 2015 11:35:09 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: insert into EMPLOYEE_DETAILS (employeeName, joiningDate) values (?, ?)
Hibernate: insert into Project (EMP_ID, projectName) values (?, ?)
Hibernate: insert into Project (EMP_ID, projectName) values (?, ?)
Mar 25, 2015 11:35:09 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost/hibernateDB]
这篇关于Hibernate:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!