gpt4 book ai didi

java - 如何在基于注释的Hibernate中将主键设置为同一个表中的外键?

转载 作者:行者123 更新时间:2023-12-02 07:22:46 26 4
gpt4 key购买 nike

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

    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;
}

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

最佳答案

假设您可以编写 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;

}

关于java - 如何在基于注释的Hibernate中将主键设置为同一个表中的外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14000241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com