如何在基于注释的Hibernate中将主键作为外键放在同一个表中?

时间:2012-12-22 05:09:08

标签: java sql hibernate

在我的基于Hibernate Annotation的应用程序中...我正在创建这样的表..

    CREATE  TABLE IF NOT EXISTS `EC_USER` (
  `S_ID` VARCHAR(30) NOT NULL PRIMARY KEY,
`CREATED_DATE` DATETIME NULL ,
`MODIFIED_BY` VARCHAR(30) NULL ,
 CONSTRAINT `CREATED_BY`
    FOREIGN KEY (`CREATED_BY` )
    REFERENCES `EC_USER` (`S_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `MODIFIED_BY`
    FOREIGN KEY (`MODIFIED_BY` )
    REFERENCES `EC_USER` (`S_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)

所以请写我的Hibernate Bean ......

@Entity
@Table(name = "EC_USER")
public class UserVO{
@Id
@Column(name="S_ID")
private String id;
@Column(name = "CREATED_DATE")
private Date createdDate;
@Column(name = "LAST_MODIFIED_DATE")
private Date modifiedDate;
}

所以这是正确的一个或任何关系映射必需...

1 个答案:

答案 0 :(得分:2)

假设您可以编写CREATED_BY和MODIFIED_BY列

@Entity
@Table(name = "EC_USER")
public class UserVO{
@Id
@Column(name="S_ID")
private String id;
@Column(name = "CREATED_DATE")
private Date createdDate;
@Column(name = "LAST_MODIFIED_DATE")
private Date modifiedDate;

@ManyToOne
@JoinColumn(name="created_by")
private UserVO createdBy; 


@ManyToOne
@JoinColumn(name="modified_by")
private UserVO modifiedBy; 

}