我遇到了这个问题但无法解决。
我想实现OneToMany / ManyToOne映射而不创建额外的表。
我有两个实体:
(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;
}
}
我有一个主要课程:
(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();
}
}
当我运行这个时,我在控制台中遇到了这个问题。
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'
有什么建议我在这做错了什么?
答案 0 :(得分:2)
代码中没有问题。我从数据库中删除了所有表,然后运行MainClass并且程序运行成功。 :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]