0

I'm using Hibernate 4.1.0.Final, Spring 3.1.1.RELEASE, and MySQL 5.5. I have this entity ...

@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;

How do I annotate the "contracts" field such that if a Contract object is deleted, its corresponding record in the join table ("user_contracts") is also removed and the user entity is properly updated? I would like the field to reflect the MySQL table declaration …

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

Thanks, - Dave

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0

try

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

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42