我想删除具有相同 ExternalProcessedFileInfo 的 ExternalProcessed 文件,但此查询失败。当关系恢复为 @onetomany 并在删除时级联时很容易,但我没有找到 ManytoOne 关系的任何有用示例。
这是我要运行的代码,它将针对选择查询运行。
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(..)
。
关于java - JPA manytoone关系删除操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6265541/