【发布时间】:2017-02-15 12:00:08
【问题描述】:
我正在尝试批量删除。
Query queryDeleteEntries = entityManager.createQuery("DELETE from
Entry r where (r.person.emailAddress like :name) and (r.otherData is null)");
int deletedEntriesCount = queryDeleteEntries.setParameter("name",
"tester." + emailAddressSuffix).executeUpdate();
我收到一个错误:
Caused by: java.sql.SQLException: ORA-00933: SQL command not properly ended
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) ~[DatabaseError.class:Oracle JDBC Driver version - "10.2.0.2.0"]
如果没有“and r.otherData is null”,它可以工作,所以我看不出有什么问题?
otherData 是一个列表,它在 Entry 类中以这种方式定义:
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "ENTRY_ID", referencedColumnName = "ID")
private List<OtherData> otherData;
OtherData(改名)类:
@Entity
@Table(name = "OTHERDATA")
@SequenceGenerator(name = "SEQ", allocationSize = 1, sequenceName = "SEQ_OTHERDATA_ID")
public class OtherData {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
private Long id;
...
@Column(name = "ENTRY_ID")
private Long entryId;
我不能在列表中使用“is null”吗?
编辑
我尝试使用“size(r.otherData) = 0”和“r.otherData IS EMPTY”,但都导致了相同的异常。
它的作用是:
Query queryDeleteEntries = entityManager.createQuery("DELETE from Entry r where id in "
+ "(select id from Entry r2 where r2.person.emailAddress like :name"
+ " and not exists (select 1 from OtherData a where a.entryId = r2.id))");
int deletedEntriesCount = queryDeleteEntries.setParameter("name", "tester."+emailAddressSuffix).executeUpdate();
但我认为它不应该那么复杂!没有更简单的方法吗?
提前感谢任何提示!
【问题讨论】:
-
我尝试了建议的“是空的”,但我得到了同样的错误。