There are tables "one" and "two." The connection between them is "many-to-one". It is necessary to insert into table Один and table Два . But if in the table Два there is already an entry, then insert nothing into it, and in the corresponding field of the table Один , insert the record identifier from table Два .

Found a good example . The only drawback is that the group I need in the second table only works within one transaction, i.e. the data that is already in the table is not taken into account (perhaps hibernate stupidly compares them by hash codes so that there are no duplicates of one object).

How to organize a record in a table with a many-to-one connection?

    1 answer 1

    Your link provides a completely working one-time example.

    A bit of theory about org.hibernate.Session methods:

    save(Object) - saves the object in the database, generating a new Id, even if it is installed. Object can be transient or detached .

    update(Object) - updates the object in the database, transforming it into persistent (Object in the detached status).

    saveOrUpdate(Object) - calls save() or update() .

    Most likely, in your application, the save(Object) method is called several times to save(Object) the same entity (the hashes are the same, and if there are any restrictions in the tables, you will get the SqlExceptions). Try using the saveOrUpdate(Object) method. When you call saveOrUpdate(Object) , hibernate will decide to add (insert) or update (update) an existing entity in the table.

    Additional terminology for disclosing topics.

    An entity object can be in four states: transient , persistent , detached , removed .

    Transient object is created with the new command and can be filled with data, but it has never been saved in a hibernate session, i.e. not associated with a row in a database table.

    Persistent object is an object currently associated with some hibernate session. The session works with an object instance at a given time interval, i.e. it is assumed that an entity object is associated with a row in a database table. You can get a persistent object in two ways, the 1st method: query the object using the following methods: get() , load() , list() , uniqueResult() , iterate() , scroll() , find() or getReference() . 2nd way: translate our transient entity object into a persistent object by calling methods such as save() , saveOrUpdate() , persist() or merge() .

    Detached object is a persistent object detached from the session. This object state occurs after the close() session is closed, which has worked with the object before this or when calling evict() or clear() session methods. The transition from the detached state back to the persistent state of an entity object is possible when calling the session update() , saveOrUpdate() or merge() methods.

    Removed object is a persistent entity object deleted in session by the delete() or remove() method in jpa. After applying the transaction, the object will be deleted from the database table.

    Detached objects can be used further when working with a new session. If such commands as update() , saveOrUpdate() or merge() applied to them, they become persistent as mentioned above. In Hibernate, such operations are called reattached mode or merging mode in jpa.