0

我正在使用 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;

如何注释“contracts”字段,以便如果删除 Contract 对象,其在连接表(“user_contracts”)中的相应记录也被删除并正确更新用户实体?我希望该字段反映 MySQL 表声明...</p>

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

谢谢, - 戴夫

4

1 回答 1

0

尝试

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

于 2013-01-16T18:09:35.157 回答