以一对一的关系删除持久化对象

时间:2015-04-01 12:04:12

标签: java openjpa

我使用openjpa作为映射层,我有两个模型: 用户:

@Entity
@Table(name = "USERS")
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "user_id_gen")
    @SequenceGenerator(name = "user_id_gen", sequenceName = "manage.users_id", allocationSize=1)
    @Column(name = "id", nullable = false)
    private int _id;

    @OneToOne (cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name="fk_image_id", insertable=true, updatable=true, nullable = true)
    private Image _image;

和图片

@Entity
@Table(name = "IMAGES")
public class Image
{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "images_id_gen")
    @SequenceGenerator(name = "images_id_gen", sequenceName = "manage.image_id", allocationSize=1)
    @Column(name = "id")
    private int _id;

它是单向的一对一关系。

问题是我想在不删除用户的情况下删除图像,当我删除用户时删除与他关联的图像。

现在,当我删除图像时,它也会删除用户!!

1 个答案:

答案 0 :(得分:0)

找到解决方案。 我将image字段设置为null并保存用户对象,然后删除图像。