JoinTable上级联删除的适当注释是什么?

时间:2013-01-16 16:07:02

标签: hibernate annotations cascading-deletes jointable

我正在使用Hibernate 4.1.0.Final,Spring 3.1.1.RELEASE和MySQL 5.5。我有这个实体......

@GenericGenerator(name = "uuid-strategy", strategy = "uuid.hex")
@Entity
@Table(name = "user")
@Cacheable
public class User implements Serializable
{
    …    
    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_contracts",
              joinColumns = @JoinColumn(name = "user_id"),
              inverseJoinColumns = @JoinColumn(name = "contract_id"))
    /* List of contracts for which this user has registered. */
    private Set<Contract> contracts;

如何对“合同”字段进行注释,以便在删除Contract对象时,还会删除连接表中的相应记录(“user_contracts”)并正确更新用户实体?我希望该字段能够反映MySQL表声明......

CREATE TABLE `user_contracts` (
  `USER_ID` varchar(32) COLLATE utf8_bin NOT NULL,
  `CONTRACT_ID` varchar(32) COLLATE utf8_bin NOT NULL,
  UNIQUE KEY `UK_USER_CONTRACTS` (`USER_ID`,`CONTRACT_ID`),
  KEY `FK2_USER_CONTRACTS` (`CONTRACT_ID`),
  CONSTRAINT `FK1_USER_CONTRACTS` FOREIGN KEY (`USER_ID`) REFERENCES `user` (`ID`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `FK2_USER_CONTRACTS` FOREIGN KEY (`CONTRACT_ID`) REFERENCES `contract` (`ID`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

谢谢, - 戴夫

1 个答案:

答案 0 :(得分:0)

尝试

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.DELETE)