0

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...

2
  • you could force it to use default value: stackoverflow.com/questions/1126610/… Commented Dec 10, 2012 at 11:07
  • Well, I think it is not the case, because the folder_type column is actually a foreign key (by means), that is why there is no default value. But, thank you for the idea =) Commented Dec 10, 2012 at 11:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.