在Helper表上删除级联

时间:2012-02-06 12:43:46

标签: hibernate hibernate-mapping

删除许可证或用户时,我想删除tbl_license_user_alert中的所有行:http://www.img-teufel.de/uploads/Unbenannt1d4f8a349jpg.jpg

我需要在哪里设置级联内容?

级联属性或@Cascade注释?

为此目的我需要哪种CascadeStyle?

@Entity
@Table( name = "tbl_license" )
public class License implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue
  @Column( name = "id", nullable = false, columnDefinition = "serial" )
  private int id;

  @OneToMany( fetch = FetchType.LAZY, mappedBy = "id.license" )
  private List<LicenseUserAlert> alerts;

  // getter + setter
}

@Entity
@Table( name = "tbl_license_user_alert" )
@AssociationOverrides({
  @AssociationOverride( name = "id.user", joinColumns = @JoinColumn( name = "user_id", columnDefinition = "int" ) ),
  @AssociationOverride( name = "id.license", joinColumns = @JoinColumn( name = "license_id", columnDefinition = "int" ) )
})
public class LicenseUserAlert implements Serializable
{
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  private LicenseUserAlertId id;

  @Column( name = "timer", columnDefinition = "int default 86400" )
  private int timer = 86400;

  public LicenseUserAlertId getId()
  {
    return id;
  }

  public void setId( LicenseUserAlertId id )
  {
    this.id = id;
  }

  @Transient
  public License getLicense()
  {
    return id.getLicense();
  }

  public void setLicense( License license )
  {
    id.setLicense( license );
  }

  @Transient
  public User getUser()
  {
    return id.getUser();
  }

  public void setUser( User user )
  {
    id.setUser( user );
  }

  public int getTimer()
  {
    return timer;
  }

  public void setTimer( int timer )
  {
    this.timer = timer;
  }
}

@Embeddable
public class LicenseUserAlertId implements Serializable
{
  private static final long serialVersionUID = 1L;

  @ManyToOne
  @ForeignKey( name = "tbl_license_fkey" )
  private License license;

  @ManyToOne
  @ForeignKey( name = "tbl_user_fkey" )
  private User user;

  public LicenseUserAlertId( License license, User user )
  {
    this.license = license;
    this.user = user;
  }

  public LicenseUserAlertId()
  {
  }

  public License getLicense()
  {
    return license;
  }

  public void setLicense( License license )
  {
    this.license = license;
  }

  public User getUser()
  {
    return user;
  }

  public void setUser( User user )
  {
    this.user = user;
  }
}

1 个答案:

答案 0 :(得分:2)

如果您正在使用JPA,请尝试将以下注释添加到License.alerts。

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id.license", cascade = { CascadeType.REMOVE }, orphanRemoval = true)
private List<LicenseUserAlert> alerts;

修改

orphanRemoval来自jpa2.0。 OneToMany注释。 (javax.persistence.OneToMany)

如果你没有jpa2.0,那么你可以通过hibernate级联注释补充jpa级联。:

@OneToMany( .... cascade = {CascadeType.REMOVE} )
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<LicenseUserAlert> alerts;

实际上这个cascadeType在以后的hibernate版本中已被弃用。请参阅Hibernate 3.6.8的源代码:

/** @deprecated use @OneToOne(orphanRemoval=true) or @OneToMany(orphanRemoval=true) */
    @Deprecated DELETE_ORPHAN,