There are 2 related tables:

CREATE TABLE IF NOT EXISTS engines ( id SERIAL PRIMARY KEY, model VARCHAR(25) UNIQUE NOT NULL, power INTEGER NOT NULL ); CREATE TABLE IF NOT EXISTS cars ( id SERIAL PRIMARY KEY, mark VARCHAR(25) NOT NULL, model VARCHAR(25) NOT NULL, engine_id INTEGER NOT NULL, FOREIGN KEY (engine_id) REFERENCES engines (id) ); 

They are related to many-to-one . One type of engine can be used in different cars. My task is to make it so that when I add a Car with the engine that is already in the engines table, a new car with the appropriate engine_id . And only if there is no such engine yet, the engine was added. But so far it turns out that Hibernate is trying to add an engine without checking if it is in engines and I get:

ERROR: duplicate key value violates unique constraint "engines_model_key" Detail: Key (model) = (test) already exists.

Which is understandable, since I myself put the model VARCHAR(25) UNIQUE in engines .

Here are my hbm.xml :

Car:

 <hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping"> <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"/> <!--Прикрепляем сущьность двигателя--> <many-to-one name="engine" column="engine_id" class="ru.javavision.model.Engine" cascade="save-update" not-null="true"/> </class> </hibernate-mapping> 

Engine:

 <hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping"> <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"/> </class> </hibernate-mapping> 

I make requests to the database through SessionFactory :

 private final SessionFactory factory = ...; public void create(@NotNull final Car car) { try (final Session session = factory.openSession()) { session.beginTransaction(); session.save(car); session.getTransaction().commit(); } } 

The question is: can I, without changing session.save(car) only due to the configuration in <many-to-one name="engine" column="engine_id" class="ru.javavision.model.Engine" cascade="save-update" not-null="true"/> or by changing only the hbm.xml configuration to configure the behavior I need. If so, how. Probably I need to change cascade="save-update" but the typing method hasn’t given anything yet.

I would appreciate any help. Thank.

  • If I understand correctly, are you trying to create an instance of Car that contains an Engine? And when you make a save game, it gives an error, because while saving the base swears that the Engine already exists? To avoid this, first make the Engine fetch (select engine from engine where model ...) Then substitute this Engine in Car and save. - aleshka-batman
  • @ aleshka-batman but if you first select the engine, this is understandable. And if you immediately save and set the behavior through the configuration. - Pavel

0