Postgres/hibernate运算符不存在:text = bytea

noo*_*der 5 java postgresql hibernate

我是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.
Run Code Online (Sandbox Code Playgroud)

这是我的hibernate映射(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>
Run Code Online (Sandbox Code Playgroud)

在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
);
Run Code Online (Sandbox Code Playgroud)

这是我的模型类(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;
}
Run Code Online (Sandbox Code Playgroud)

在我的DAO层中,我使用以下行来查询:

Query query = getSession().createQuery("from Car as c where " +
   "c.carModel = ? AND c.carMake = ?").setParameter(0, carModel).setParameter(1, carMake);
Run Code Online (Sandbox Code Playgroud)

carMake和carModel都是作为DAO方法中的方法参数传递的String数据类型.

请注意,我的hbm中的字符串映射到postgres中的TEXT,所以我猜这是不是问题.如果是,我该如何解决?

noo*_*der 6

这很奇怪,但查询不能很好地处理null.当我将查询更改为:

Query query = getSession().createQuery("from Car as c where " +
"c.carModel = ? AND c.carMake is null").setParameter(0, carModel);
Run Code Online (Sandbox Code Playgroud)

它工作正常,因为DAO需要将make查询为NULL.因此,如果它不是null,我需要有两组查询,一组是硬编码,如上所述选择null,另一组是setParam(1,carMake).

很奇怪,但我觉得这很有效.