美好时光。
说,有两个数据库表: FOLDERS 和 FOLDER_TYPES 。
CREATE TABLE FOLDERS (
ID VARCHAR2(50 BYTE) NOT NULL,
NAME VARCHAR2(4000 BYTE) NOT NULL,
DESCRIPTION VARCHAR2(4000 BYTE),
FOLDER_TYPE_ID VARCHAR2(50 BYTE) NOT NULL,
PARENT_FOLDER_ID VARCHAR2(50 BYTE)
)
CREATE TABLE FOLDER_TYPES (
ID VARCHAR2(50 BYTE) NOT NULL,
NAME VARCHAR2(50 BYTE) NOT NULL,
DESCRIPTION VARCHAR2(4000 CHAR)
)
正如您所见, FOLDER_TYPE_ID 列的 FOLDERS 表格中存在 NOT NULL约束。
这些表的Hibernate实体如下:
@Entity
@Table(name = "folders")
public class Folder implements Serializable {
private static final long serialVersionUID = 1856858081142422000L;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "[ID]")
private String id;
@Column(name = "[NAME]")
private String name;
@Column(name = "[DESCRIPTION]")
private String description;
@ManyToOne
@JoinColumn(name = "folder_type_id")
private FolderType type;
...
和
@Entity
@Table(name = "folder_types")
public class FolderType implements Serializable{
private static final long serialVersionUID = -7992732910184226857L;
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "[ID]")
private String id;
@Column(name = "[NAME]")
private String name;
@Column(name = "[DESCRIPTION]")
private String description;
问题是:当我删除Folder对象时,在从DB实际删除之前,Hibernate尝试为引用(文件夹类型)设置一个空值,但是有约束,所以抛出异常。作为一种解决方法,我可以删除约束,但如果需要它,我不能改变表格( FOLDERS )?
有没有办法说Hibernate不将实体更新为null引用?我已经尝试将updatable = false
设置为文件夹实体中的@JoinColumn
(FolderType类型),但是没有运气...