JPA多关系删除操作

时间:2011-06-07 13:00:47

标签: java jpa many-to-one

我想删除ExternalProcessed文件具有相同的ExternalProcessedFileInfo但是此查询失败。当Relation恢复为@onetomany并在删除时级联时很容易,但我没有找到任何有用的ManytoOne关系示例。

这是我想要运行的代码,它将运行select select。

javax.persistence.Query query =this. manager.createQuery("Delete  from  ExternalProcessedFile "
                + " f WHERE f.processInfo.source.name= :source ");
          query.setParameter("source",source.getName()) ;
          EntityTransaction tran=  manager.getTransaction();
          try{
           tran.begin();
          query.executeUpdate();
          tran.commit();    

@Entity
@Table(name = "ProcessedFile")

public class ExternalProcessedFile implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "EID")
    private Long id;


     @NotNull
     @ManyToOne
     private  ExternalProcessedFileInfo processInfo;  

@Entity
@Table(name = "ProcessedFileInfo")
public class ExternalProcessedFileInfo implements Serializable {

    public ExternalProcessedFileInfo(){

    }
     public ExternalProcessedFileInfo(String processtime,ExternalDataStorage source){
        this.processTime=processtime;
        this.source=source;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "EID")
    private Long id;


    @ManyToOne
    @JoinColumn
    private ExternalDataStorage  source;


    @NotEmpty
    @Column(name = "processtime")
    private String processTime;

1 个答案:

答案 0 :(得分:1)

DELETE查询不考虑级联。您必须获取每个实体并entityManager.remove(..)