@OneToOne and @JoinColumn, auto delete null entity , doable?

Posted by smallufo on Stack Overflow See other posts from Stack Overflow or by smallufo
Published on 2010-06-14T05:48:06Z Indexed on 2010/06/14 5:52 UTC
Read the original article Hit count: 218

Filed under:
|
|
|

I have two Entities , with the following JPA annotations :

@Entity
@Table(name = "Owner")
public class Owner implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  private long id;

  @OneToOne(fetch=FetchType.EAGER , cascade=CascadeType.ALL)
  @JoinColumn(name="Data_id")
  private Data Data;  
}

@Entity
@Table(name = "Data")
public class Data implements Serializable
{
  @Id
  private long id;
}

Owner and Data has one-to-one mapping , the owning side is Owner. The problem occurs when I execute : owner.setData(null) ; ownerDao.update(owner) ; The "Owner" table's Data_id becomes null , that's correct.

But the "Data" row is not deleted automatically. I have to write another DataDao , and another service layer to wrap the two actions ( ownerDao.update(owner) ; dataDao.delete(data); )

Is it possible to make a data row automatically deleted when the owning Owner set it to null ?

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about orm