删除 - ManyToOne - 实体仍然存在

时间:2014-02-11 09:29:15

标签: java hibernate h2

我是Hibernate的新手,我已经开始做一个小项目了。 我想测试添加和删除所有类型的关系。 我不明白如何管理删除...(对于ManyToMany它似乎运作良好) (我使用H2数据库)

我有一个ManyToOne关系: WORK有0或很多步骤,所以:(最后是java和sql)

我创建了一些实体,然后我只想从数据库中删除一个工作......

详情

我创建了一个Junit to Test,我显示了我的数据库:

- save new work : "workOne"
- save new work : "workTwo"
- save new file : "fileone"
- save new step : "stepOne" 
  - stepOne.setWork(workOne)
- save new step : "stepTwo" 
  - stepOne.setWork(workOne)
  - stepOne.setFile(fileOne)

保存:

entityManager.getTransaction().begin();
entityManager.save(object);
entityManager.getTransaction().commit();

显示(通过数据库上的sql请求)>>>好的,我的所有实体都显示出良好的关联:

workOne - ID = 10
workTwo - ID = 11

fileOne - ID = 20

stepOne - ID = 40 / ID_WORK = 10 / ID_DOS = null
stepTwo - ID = 41 / ID_WORK = 11 / ID_DOS = 20

现在我想测试删除作品(在同一个测试中)

// Remove a work
    entityManager.getTransaction().begin();
    entityManager.remove(workOne);
    entityManager.getTransaction().commit();
// Display BDD
>> The workOne has not been deleted from database ... (no hibernate delete on logs)

我期待:

  1. workOne(#10)将被删除
  2. stepOne也是......(因为我们不能没有工作的一步)
  3. 谢谢,

    java:

    @Entity
    @Table(name = "T_WORK")
    public class Work {
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private long id = 0L;
      @Column(name = "LABEL")
      String label;
    
    @Entity
    @Table(name = "T_STEP")
    public class Step{
    
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      private long id = 0L;
    
      @ManyToOne(cascade = {CascadeType.ALL })
      @JoinColumn(name = "ID_WORK")
      Work work;
    
      @OneToOne(cascade = {CascadeType.ALL })
      @JoinColumn(name = "ID_FILE")
      File file;
    
      @Column(name = "LABEL")
      private String label;
      @Column(name = "STATUS")
      private String status;
    

    h2.sql:

    create table T_WORK (
    ID number(10) not null,
    LABEL varchar2(64) not null,
    constraint PK_WORK primary key (ID)
    );
    
    create table T_STEP (
    ID number(10) not null,
    ID_WORK number(10) not null,
    ID_FILE number(10),
    LABEL varchar2(64) not null,
    STATUS varchar2(64),
    constraint PK_STEP primary key (ID),
    CONSTRAINT FK_STEPS_WORK FOREIGN KEY (ID_TRA) REFERENCES T_WORK(ID),
    CONSTRAINT FK_STEP_FILE FOREIGN KEY (ID_DOS) REFERENCES T_FILE(ID)
    );
    

0 个答案:

没有答案