1

我正在开发一个项目并使用 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;
}

这是我得到的例外:

休眠:插入 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.service() 用于 servlet 操作引发异常

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)

插入查询甚至没有这两列。这就是查询的方式。

休眠:插入 PROJECT_MASTER(PROJECT_NAME、PROJECT_NUMBER、START_DATE、END_DATE、PROJECT_MANAGET_ID、PROJECT_SUPERVISOR_ID、SUBMITTED_BY、SUBMITTED_DATE、UPDATED_BY)值(?、?、?、?、?、?、?、?、?)

4

1 回答 1

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_B (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_PROFILE) USER_IDREFERENCES ), 外键 (UPDATED_BY) 参考 USER_PROFILE(USER_ID) );

你已经让你的班级使用这个表。因为你没有一个NOT NULL字段的默认值,Hibernate 抱怨。从表中删除该列或在实体映射中添加关系。

改成

@ManyToOne
@JoinColumn(name="PROJECT_MANAGER_ID ",insertable=false, updatable=false,
        nullable=false)
private UserProfileBean projectManagerID;
于 2013-09-12T18:45:30.337 回答