12

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.

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):

<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:

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):

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:

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.

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 ?

8
  • This cannot be answered without seeing the definition of the model class. Commented Nov 25, 2015 at 22:02
  • Please, show us our the query you are using. Commented Nov 25, 2015 at 22:05
  • @Daniel added the model class Commented Nov 25, 2015 at 22:09
  • @Bonifacio added the query Commented Nov 25, 2015 at 22:09
  • Is it g.carMake or c.carMake in the query? Commented Nov 25, 2015 at 22:11

5 Answers 5

11

It is weird but the query does not handle null very well. When I changed the query to:

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).

Weird but I think this works.

Sign up to request clarification or add additional context in comments.

Comments

5

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).

Map the Date! Use @Temporal(Date) on the Date attribute. I don't know how to express that in hbm.xml notation.

2 Comments

But if I've specified it in my hbm.xml correctly, I shoudn't need to explicitly mention it with annotations, right ?
Correct. Annotations and hbm.xml should have exactly the same possibilities.
4

Use Query.setParameterList instead of setParameter solved my problem for an integer array (integer = bytea)

Comments

0

I had the same problem and in my case it was caused by the fact that I was trying to use a Java enum class as a named parameter within a native query.

The solution was to go all the way with Hibernate, so non-native query and using Java class and member names instead of table name and column names.

Comments

0

Had this error when I used a native query with in operator on a Enum field which is text in PostgreSQL. Converting the list of Enum to a list of String made this error go away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.