[英]How to use query operators && or @> when creating a native query in hibernate
CREATE TABLE IF NOT EXISTS posts(
postid SERIAL NOT NULL,
owner SERIAL NOT NULL,
title VARCHAR(100) UNIQUE NOT NULL,
message VARCHAR(500) NOT NULL,
continent VARCHAR(100) NOT NULL,
country VARCHAR(100) NOT NULL,
state VARCHAR(100) NOT NULL,
languages VARCHAR(100)[] NOT NULL,
properties varchar(100) ARRAY NOT NULL,
PRIMARY KEY (postid),
FOREIGN KEY (owner) REFERENCES users(userid) ON DELETE CASCADE
);
final Query query = em.createNativeQuery("SELECT postid FROM posts WHERE :properties <@ properties");
query.setParameter("properties", propAux);
I'm migrating my project to hibernate (I'm using the version 1.0.0.Final of javax.persistence.Query), and I can't seem to run that query.我正在将我的项目迁移到 hibernate(我使用的是 javax.persistence.Query 的 1.0.0.Final 版本),但我似乎无法运行该查询。 It says that
<@
operator doesn't work with types bytea
and suggests to cast it.它说
<@
运算符不适用于类型bytea
并建议强制转换它。 I tried casting it to text[]
, replacing :properties <@ properties
with cast(:properties as text[]) <@ cast(properities as text[])
but didn't work either.我尝试将其转换为
text[]
,将:properties <@ properties
替换为cast(:properties as text[]) <@ cast(properities as text[])
但也不起作用。
Any suggestions?有什么建议么?
PS: propAux
is an array of Strings. PS:
propAux
是一个字符串数组。
Assuming that you have the following setup:假设您有以下设置:
create table TST_POSTS
(
postid int,
properties varchar(200)[],
primary key (postid)
);
insert into TST_POSTS
values (1, '{"aaa", "bbb"}'), (2, '{"aaa", "ccc"}'), (4, '{"ddd", "ccc"}');
You can write the following query:您可以编写以下查询:
import org.hibernate.jpa.TypedParameterValue;
import com.vladmihalcea.hibernate.type.array.StringArrayType;
// ...
List<Integer> results = entityManager
.createNativeQuery("select postid from TST_POSTS where :prop <@ cast(properties as text[])")
.setParameter("prop", new TypedParameterValue(StringArrayType.INSTANCE, new String[] {"ccc"}))
.getResultList();
and you will get the output:你会得到 output:
2
4
This approach requires to have hibernate-types as a dependency.这种方法需要将休眠类型作为依赖项。
PS I tested it with hibernate-types-52
(version 2.10.1) and hibernate 5.4.29.Final. PS我用
hibernate-types-52
(版本2.10.1)和hibernate 5.4.29.Final对其进行了测试。 See also this article .另请参阅这篇文章。
EDIT编辑
You can use the following query for hibernate 5.1.0.Final:您可以对 hibernate 5.1.0.Final 使用以下查询:
import org.hibernate.Query;
import com.vladmihalcea.hibernate.type.array.StringArrayType;
// ...
Query query = entityManager
.createNativeQuery("select postid from TEST_SCHEMA.TST_POSTS where :prop <@ cast(properties as text[])")
.unwrap(Query.class)
.setParameter("prop", new String[] {"ccc"}, StringArrayType.INSTANCE);
@SuppressWarnings("unchecked")
List<Integer> result = query.list();
and you should use the following dependency:并且您应该使用以下依赖项:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-5</artifactId>
<version>2.11.1</version>
</dependency>
instead of hibernate-types-52
.而不是
hibernate-types-52
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.