Helper 表上的级联删除

发布于 2025-01-02 20:39:22 字数 2515 浏览 2 评论 0原文

我想在删除许可证或用户时删除 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;
  }
}

I would like to delete all rows in tbl_license_user_alert when deleting either a license or an user: http://www.img-teufel.de/uploads/Unbenannt1d4f8a349jpg.jpg

Where do I need to set the cascade stuff?

Cascade Attribute or @Cascade Annotation?

Which CascadeStyle do I need for this purpose?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2025-01-09 20:39:22

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

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

编辑

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

如果你没有jpa2.0,那么你可以通过hibernatecascading注解来补充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,

try to add following annotations to your License.alerts if you are working with JPA.

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

EDIT

orphanRemoval is from jpa2.0. OneToMany annotation. (javax.persistence.OneToMany)

if you don't have jpa2.0, then you can complement jpa cascading by hibernate cascade annotation.:

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

infact this cascadeType is deprecated in later hibernate version. see source code from Hibernate 3.6.8:

/** @deprecated use @OneToOne(orphanRemoval=true) or @OneToMany(orphanRemoval=true) */
    @Deprecated DELETE_ORPHAN,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文