I have two entities Car and Engine. The Engine contains Set of Cars which use the corresponding Engine.
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Engine {
private int id;
private String model;
private int power;
private Set<Car> cars;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Car {
private int id;
private String mark;
private String model;
}
Configurations implemented in hbm.xml
format:
<class name="ru.javavision.model.Engine" table="engines">
<id name = "id" type = "int" column = "id">
<generator class="identity"/>
</id>
<property name="model" column="model"/>
<property name="power" column="power"/>
<set name="cars" table="cars">
<key column="mark" foreign-key="car_mark"/>
<one-to-many class="ru.javavision.model.Car"/>
</set>
</class>
<class name="ru.javavision.model.Car" table="cars">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="mark" column="mark"/>
<property name="model" column="model"/>
</class>
But I got the type error. But I can't figure it out why.
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = integer Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
In database reference type string on both sides:
CREATE TABLE IF NOT EXISTS cars (
id SERIAL PRIMARY KEY,
mark VARCHAR(25) UNIQUE NOT NULL,
model VARCHAR(25) NOT NULL
);
CREATE TABLE IF NOT EXISTS engines (
id SERIAL PRIMARY KEY,
model VARCHAR(25) NOT NULL,
power INTEGER NOT NULL,
car_mark VARCHAR(25) NOT NULL,
FOREIGN KEY (car_mark) REFERENCES cars (mark)
);
mark
and car_mark
is VARCHAR
but hibernate seen INTEGER
. Perhaps he trying mapping by car primary key.
Probably my mistake into Engine XML config somewhere in here:
<set name="cars" table="cars">
<key column="mark" foreign-key="car_mark"/>
<one-to-many class="ru.javavision.model.Car"/>
</set>
Perhaps foreign-key
attribute doesn't apply and foreign references to id which INTEGER
?
Help me find and anderstand my mistake. Thank You!