Good time.
Say, there are two DB tables: FOLDERS and 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)
)
As you can see there is a NOT NULL contraint in the FOLDERS table on the FOLDER_TYPE_ID column.
The Hibernate's entities for these tables are the following:
@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;
...
and
@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;
The problem is: when I remove a Folder object, before the actual deletion from the DB, Hibernate tries to set a null value to the reference (folder type), but there is the constraint, so an exception is thrown. As a workaround I can remove the constraint, but what if it is required to have it, and I can't alter the table (FOLDERS)?
Is there a way to say Hibernate not to update the entity to null references? I've tried setting updatable = false to the @JoinColumn (FolderType type) in the Folder entity, but there has been no luck...