Reasons and solutions for invalid id manually set by hibernate

In the process of using Hibernate, it is found that the manually set id (primary key) is invalid, and Hibernate will still automatically generate a random id when saving (calling the merge() method provided by Hibernate).

After debugging, I found that the problem lies in Hibernate's entity mapping configuration file (xml).

<id name="id" type="string" column="id">
    <generator class="uuid.hex"/>
</id>

Because the id automatic generation strategy is added to the id mapping field, Hibernate will ignore the manually set id and automatically generate an id according to the id automatic generation strategy.

The solution is very simple, just block this id automatic generation strategy, simple and rude.

<id name="id" type="string" column="id">
    <!-- <generator class="uuid.hex"/> -->
</id>

That solved the problem, hehe.

It should be noted that in this way, the id must be manually generated and set every time it is saved, but the id here is pushed by another system, which is a perfect solution.

 

"In my left hand is an unforgettable firefly, and in my right hand is a long forgetfulness of ten years."

Reprinted in: https://www.cnblogs.com/yanggb/p/11268679.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324840074&siteId=291194637