Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres / hibernate operator does not exist: text = bytea

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 ?

like image 637
noobcoder Avatar asked Nov 25 '15 21:11

noobcoder


1 Answers

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.

like image 152
noobcoder Avatar answered Oct 20 '22 10:10

noobcoder