【问题标题】:Postgres / hibernate operator does not exist: text = byteaPostgres / hibernate 运算符不存在:text = bytea
【发布时间】:2016-02-28 20:50:59
【问题描述】:

我是 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.

这是我的休眠映射(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>

在 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
);

这是我的模型类(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;
}

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

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

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

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

【问题讨论】:

  • 这个不看模型类的定义是无法回答的。
  • 请向我们展示您正在使用的查询。
  • @Daniel 添加了模型类
  • @Bonifacio 添加了查询
  • 查询中是g.carMake还是c.carMake?

标签: java hibernate postgresql


【解决方案1】:

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

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

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

很奇怪,但我认为这行得通。

【讨论】:

    【解决方案2】:

    通常,此错误来自 Hibernate 序列化一个未映射的类(导致一个 bytea)并将其与一个字符串(可能由您在查询中给出)进行比较。

    映射日期!在 Date 属性上使用 @Temporal(Date)。我不知道如何用 hbm.xml 表示法来表达。

    【讨论】:

    • 但是如果我在我的 hbm.xml 中正确指定了它,我应该不需要用注释明确提及它,对吧?
    • 正确。 Annotations 和 hbm.xml 应该有完全相同的可能性。
    【解决方案3】:

    使用 Query.setParameterList 而不是 setParameter 解决了我的整数数组问题 (integer = bytea)

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,在我的情况下,这是由于我试图在本机查询中使用 Java 枚举类作为命名参数而引起的。

      解决方案是一直使用 Hibernate,所以非本地查询并使用 Java 类和成员名称而不是表名和列名。

      【讨论】:

        猜你喜欢
        • 2018-02-17
        • 2021-02-24
        • 2020-08-24
        • 2023-01-08
        • 2016-06-06
        • 2012-05-19
        • 2014-10-11
        • 2020-02-05
        相关资源
        最近更新 更多