JPA多对一关系删除操作



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

这是我想要运行的代码,它将为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;
DELETE查询不考虑级联。您必须获取每个实体并将其entityManager.remove(..)

最新更新