postgresql Postgres / hibernate 操作符不存在:text = bytea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33927108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgres / hibernate operator does not exist: text = bytea
提问by noobcoder
I am new to the hibernate world and I am getting the following error message when trying to execute a query with hibernate and postgres.
我是 hibernate 世界的新手,尝试使用 hibernate 和 postgres 执行查询时收到以下错误消息。
org.postgresql.util.PSQLException: ERROR: operator does not exist: text = bytea
Hint: No operator matches the given name and argument type(s). You might
need to add explicit type casts.
Here is my hibernate mapping (car.hbm.xml):
这是我的休眠映射(car.hbm.xml):
<hibernate-mapping>
<class name="Car" table="car"
schema="someSchema">
<id name="id" type="int" column="car_id">
<generator class="sequence">
<param name="sequence">car_seq</param>
</generator>
</id>
<property name="carMake">
<column name="car_make" sql-type="string"/>
</property>
<property name="carModel">
<column name="car_model" sql-type="string"/>
</property>
<property name="carVin" >
<column name="car_vin" sql-type="int" />
</property>
<property name="datePurchased">
<column name="date_purchased" sql-type="date"/>
</property>
<property name="retiredModel">
<column name="retired_model" sql-type="boolean"/>
</property>
</class>
On Postgres, here is what my table looks like:
在 Postgres 上,这是我的表的样子:
CREATE TABLE car (
car_vin INTEGER NOT NULL DEFAULT nextval('car_seq'::regclass) PRIMARY KEY,
car_make TEXT NOT NULL,
car_model TEXT DEFAULT NULL,
date_purchased DATE DEFAULT now() NOT NULL,
retired_model BOOLEAN DEFAULT FALSE NOT NULL
);
Here is my model class (Car.java):
这是我的模型类(Car.java):
public class Car {
private int id;
private String carMake;
private String carModel;
private int carVin;
private Date datePurchased;
private boolean retiredModel;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCarModel() {
return carModel;
}
public void setcarModel(String carModel) {
this.carModel = carModel;
}
public String getcarMake() {
return carMake;
}
public void setcarMake(String carMake) {
this.carMake = carMake;
}
public Date getDatePurchased() {
return datePurchased;
}
public void setDatePurchased(Date datePurchased) {
this.datePurchased = datePurchased;
}
public boolean isRetired() {
return retiredModel;
}
public void setRetired(boolean retiredModel) {
this.retiredModel = retiredModel;
}
In my DAO layer, I am using the following line to query:
在我的 DAO 层中,我使用以下行进行查询:
Query query = getSession().createQuery("from Car as c where " +
"c.carModel = ? AND c.carMake = ?").setParameter(0, carModel).setParameter(1, carMake);
carMake and carModel are both String datatypes passed on as method parameters in the DAO method.
carMake 和 carModel 都是在 DAO 方法中作为方法参数传递的字符串数据类型。
Note that the strings in my hbm are mapped to TEXT in postgres, so I am guessing if that is the problem or not. If it is, how do I solve it ?
请注意,我的 hbm 中的字符串映射到 postgres 中的 TEXT,所以我猜测这是否是问题所在。如果是,我该如何解决?
采纳答案by noobcoder
It is weird but the query does not handle null very well. When I changed the query to:
这很奇怪,但查询不能很好地处理 null。当我将查询更改为:
Query query = getSession().createQuery("from Car as c where " +
"c.carModel = ? AND c.carMake is null").setParameter(0, carModel);
it works fine since the DAO needs to query the make as NULL. So if it is not null, I need to have two sets of query, one that is hardcoded to select null as above, other to setParam(1, carMake).
它工作正常,因为 DAO 需要将 make 查询为 NULL。因此,如果它不为空,我需要有两组查询,一组是硬编码以选择 null 如上所述,另一组是 setParam(1, carMake)。
Weird but I think this works.
奇怪,但我认为这有效。
回答by Daniel
Usually this error is from Hibernate serializing a not-otherwise-mapped class (resulting in a bytea) and comparing that to a String (probably given by you in a query).
通常这个错误来自 Hibernate 序列化一个未映射的类(导致一个字节)并将其与一个字符串(可能由您在查询中给出)进行比较。
Map the Date! Use @Temporal(Date) on the Date attribute. I don't know how to express that in hbm.xml notation.
映射日期!在 Date 属性上使用 @Temporal(Date)。我不知道如何用 hbm.xml 表示法来表达。
回答by ?smail Yavuz
Use Query.setParameterList instead of setParameter solved my problem for an integer array (integer = bytea)
使用 Query.setParameterList 而不是 setParameter 解决了我的整数数组问题(整数 = bytea)