Cascade在Hibernate中不起作用

时间:2017-01-10 12:28:59

标签: java mysql hibernate cascade

我有一个系统,我有两种类型的设备:Staticedevice和dynamicdevice。由于我对mysql继承的了解有限,而且我不知道如何使它与hibernate一起工作,我只是做了一个BASEDEVICE。因此,staticdevice和dyndevice都有一个basekey到basedevice,我设置static和dyndevice可以为空。

The database 问题是,当我保存basedeivce时,我希望保存dyndevice和静态设备,因为我用CascadeType.Persist对它进行了注释。它不起作用,所以我尝试使用.ALL。仍然不起作用!

@Entity
@NamedQuery(name="Basedevice.findAll", query="SELECT b FROM Basedevice b")
public class Basedevice implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    //bi-directional one-to-one association to Dyndevice
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="id", referencedColumnName="BASEDEVICE_id", nullable = true)
    private Dyndevice dyndevice;

    //bi-directional one-to-one association to Staticdevice
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="id", referencedColumnName="BASEDEVICE_id", nullable = true)
    private Staticdevice staticdevice;

    //bi-directional one-to-one association to Devicemap
    @OneToOne
    @JoinColumn(name="id", referencedColumnName="BASEDEVICE_id", nullable = true)
    private Devicemap devicemap;

    //bi-directional many-to-one association to Logentry
    @OneToMany(mappedBy="basedevice", fetch = FetchType.EAGER)
    private List<Logentry> logentries;
    ....
}

为什么它不会持续我的静态或dyndevice呢? Basedevice保持良好状态,我确信它有一个静态设备:

public void addStaticDevice(Staticdevice device) {
    DAO<Basedevice> basedeviceDAO = new DAO<>(Basedevice.class);
    basedeviceDAO.save(device.getBasedevice());
    devices_.add(device.getBasedevice());
}

.save方法调用entitymanager的persist-method。 我之前从未遇到过级联问题。

有什么想法吗?

0 个答案:

没有答案