开发者

ON DELETE SET NULL ON UPDATE SET NULL using JPA

How do i add ON DELETE SET NULL ON UPDATE SET NULL Constraint while creating the table using JPA . Below the entity definition

table

CREATE TABLE `node` (
  `id` bigint(20) NOT NULL,
  `parentNode_id` bigint(20) default NULL,
  PRIMARY KEY  (`id`),
  KEY `FK1EC1DD0F28AB6BA5` (`parentNode_id`),  
  CONSTRAINT `FK1EC1DD0F28AB6BA5` FOREIGN KEY (`parentNode_id`) REFERENCES `node` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



@Entity
public class Node {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "NodeSequence")
    pr开发者_如何学JAVAivate long id;

    @ManyToOne(cascade=CascadeType.REMOVE)
    @JoinColumn(name = "parentNode_id", nullable = true)
    protected Node parentNode = null;

}


It is a bad idea to use the JPA schema generator for this purpose. It is almost always preferable to have DDL scripts that are hand-coded and placed under version control, to manage your database.

Also, setting foreign key values to null via database constraints, will only result in your persistence context having dirty values, unless the JPA provider is aware of the foreign key constraints in the database, and consciously updates the corresponding values in the persistence context to null.

Also, it is unlikely that JPA schema generators support this feature. For instance, Hibernate does not support this yet, and there are multiple feature requests that have been raised, and none of them have been resolved. EclipseLink also does not appear to support this feature.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜