[英]JPA setting UUID as query parameter
I am getting issues in executing delete query
using JPA
. 我在使用
JPA
执行delete query
时遇到问题。
The problem is that I am deleting a row based on UUID. 问题是我要删除基于UUID的行。
To accomplish delete task, I set the UUID
parameter in JPA query setParameter
method. 为了完成删除任务,我在JPA查询
setParameter
方法中设置了UUID
参数。 But this fails giving CAST
issues. 但这
CAST
问题。
So I used the following: 因此,我使用了以下内容:
query.setParameter("fileUuid", String.class.cast(fileUuid));
But again it gave exception. 但是再次给了例外。
Here is the code-- 这是代码-
Entity Class: 实体类别:
@Id
@Type(type = "pg-uuid")
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen")
@Column(name = "file_uuid", updatable = false, nullable = false)
private UUID fileUuid;
Manager Class: 经理班:
@Override
public void deleteFile(UUID fileUuid) {
final String ql = "delete from " + type.getName() + "
where file_uuid = :fileUuid";
Query query = getEntityManager().createQuery(ql);
query.setParameter("fileUuid",
String.class.cast(fileUuid));
try {
query.executeUpdate();
} catch (NoResultException ex) {
return;
}
}
Exception Log: 异常日志:
java.lang.ClassCastException: Cannot cast java.util.UUID to java.lang.String
at java.lang.Class.cast(Class.java:3369)
UPDATE: 更新:
These are the dependencies in my pom.xml- 这些是我的pom.xml-中的依赖项
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.11.Final</version>
</dependency>
Using super.removeById
is the working solution- 使用
super.removeById
是有效的解决方案-
@Override
public void deleteFile(UUID fileUuid) {
T entity =getEntityManager().find(type, id)
if (entity != null) {
getEntityManager().remove(entity);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.