gpt4 book ai didi

java - JPA manytoone关系删除操作

转载 作者:行者123 更新时间:2023-11-30 11:53:26 25 4
gpt4 key购买 nike

我想删除具有相同 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com