我正在开发一个项目并使用Hibernate进行数据库交互。
我有两个表,一个是RESOURCE_PROFILE
,第二个是PROJECT_MASTER
。以下是DDL声明。
CREATE TABLE USER_PROFILE (
FIRST_NAME VARCHAR(15) NOT NULL,
MIDDLE_NAME VARCHAR(15),
LAST_NAME VARCHAR(15) NOT NULL,
EMAIL_ID VARCHAR(40) NOT NULL,
USER_ID VARCHAR(40) NOT NULL,
PASSWORD VARCHAR(1000) NOT NULL,
ROLE VARCHAR(20),
SUPERVISOR_ID VARCHAR(20),
SUBMITTED_BY VARCHAR(20),
SUBMITTED_DATE DATE,
PRIMARY KEY (USER_ID)
);
CREATE TABLE PROJECT_MASTER (
PROJECT_ID INT NOT NULL AUTO_INCREMENT,
PROJECT_NAME VARCHAR(50) NOT NULL,
PROJECT_NUMBER VARCHAR(50) NOT NULL,
START_DATE DATE,
END_DATE DATE,
PROJECT_MANAGER_ID VARCHAR(40) NOT NULL,
PROJECT_SUPERVISOR_ID VARCHAR(40) NOT NULL,
SUBMITTED_BY VARCHAR(40),
SUBMITTED_DATE DATE,
UPDATED_BY VARCHAR(40),
PRIMARY KEY (PROJECT_ID),
FOREIGN KEY (PROJECT_MANAGER_ID) REFERENCES USER_PROFILE(USER_ID),
FOREIGN KEY (PROJECT_SUPERVISOR_ID) REFERENCES USER_PROFILE(USER_ID),
FOREIGN KEY (SUBMITTED_BY) REFERENCES USER_PROFILE(USER_ID),
FOREIGN KEY (UPDATED_BY) REFERENCES USER_PROFILE(USER_ID)
);
正如您在PROJECT_MASTER
表中所看到的,我使用USER_ID(RESOURCE_PROFILE)
作为PROJECT_MASTER
表中多列的外键。下面是我为这两个表创建的实体类。我确信我在映射中做错了,你能帮助我并指出正确的方向吗?
由于我将相同的外键用于多列,我该如何实现这种映射?
@Entity
@Table(name = "PROJECT_MASTER")
public class ProjectMasterBean {
@Id
@GeneratedValue
@Column(name="PROJECT_ID")
private int projectID;
@Column(name="PROJECT_NAME")
private String projectName;
@Column(name="PROJECT_NUMBER")
private String projectNumber;
@Column(name="START_DATE")
private Date startDate;
@Column(name="END_DATE")
private Date endDate;
@ManyToOne
@JoinColumn(name="USER_ID",insertable=false, updatable=false,
nullable=false)
private UserProfileBean projectManagerID;
@ManyToOne
@JoinColumn(name="USER_ID",insertable=false, updatable=false,
nullable=false)
private UserProfileBean projectSupervisorID;
@ManyToOne
@JoinColumn(name="USER_ID",insertable=false, updatable=false,
nullable=false)
private UserProfileBean submittedBy;
}
@Column(name="SUBMITTED_DATE")
private Date submittedDate;
@ManyToOne
@JoinColumn(name="USER_ID",insertable=false, updatable=false,
nullable=false)
private UserProfileBean updatedBy;
public class UserProfileBean {
@Id
@Column(name="USER_ID")
private String userID;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="MIDDLE_NAME")
private String middleName;
@Column(name="LAST_NAME")
private String lastName;
@Column(name="EMAIL_ID")
private String emailID;
@Column(name="PASSWORD")
private String password;
@Column(name="ROLE")
private String userRole;
@Column(name="SUPERVISOR_ID")
private String supervisorID;
@Column(name="SUBMITTED_BY")
private String submittedBy;
@Column(name="SUBMITTED_DATE")
private String submittedDate;
}
这是我得到的例外:
Hibernate:插入PROJECT_MASTER(PROJECT_NAME,PROJECT_NUMBER,START_DATE,END_DATE,SUBMITTED_BY,SUBMITTED_DATE,UPDATED_BY)值(?,?,?,?,?,?,?) 2013年9月12日下午8:46:20 org.apache.catalina.core.StandardWrapperValve调用 严重:servlet操作的Servlet.service()抛出异常
java.sql.SQLException: Field 'PROJECT_MANAGER_ID' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
Insert Query甚至没有这两列。这就是查询的方式。
Hibernate:插入PROJECT_MASTER(PROJECT_NAME,PROJECT_NUMBER,START_DATE,END_DATE,PROJECT_MANAGET_ID,PROJECT_SUPERVISOR_ID,SUBMITTED_BY,SUBMITTED_DATE,UPDATED_BY)值(?,?,?,?,?,?,?,?,?)
答案 0 :(得分:1)
您已使用
创建了表格CREATE TABLE PROJECT_MASTER(PROJECT_ID INT NOT NULL AUTO_INCREMENT, PROJECT_NAME VARCHAR(50)NOT NULL,PROJECT_NUMBER VARCHAR(50)NOT NULL,START_DATE DATE,END_DATE DATE, PROJECT_MANAGER_ID VARCHAR(40) NOT NULL ,PROJECT_SUPERVISOR_ID VARCHAR(40)NOT NULL,SUBMITTED_BY VARCHAR(40),SUBMITTED_DATE DATE,UPDATED_BY VARCHAR(40),PRIMARY KEY (PROJECT_ID), FOREIGN KEY(PROJECT_MANAGER_ID)参考 USER_PROFILE(USER_ID),FOREIGN KEY(PROJECT_SUPERVISOR_ID)参考 USER_PROFILE(USER_ID),FOREIGN KEY(SUBMITTED_BY)参考 USER_PROFILE(USER_ID),FOREIGN KEY(UPDATED_BY)参考 USER_PROFILE(USER_ID));
你让你的班级使用这个表。因为你没有NOT NULL
字段的默认值,所以Hibernate会抱怨。从表中删除该列或在实体映射中添加关系。
更改为
@ManyToOne
@JoinColumn(name="PROJECT_MANAGER_ID ",insertable=false, updatable=false,
nullable=false)
private UserProfileBean projectManagerID;