JPA manytoone relation delete operation
I want to delete ExternalProcessed files has same ExternalProcessedFileInfo But this query fails.It is easy when Relation reverted to @onetomany and cascade on delete but i didn't find any useful example for ManytoOne relations.
This is the code i want to run and it will run for select que开发者_如何学Pythonry .
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 queries do not take cascades into account. You will have to fetch each entity and entityManager.remove(..)
it.
精彩评论