Hibernate manually set id invalid reason and solution

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

After debugging, it is found that the problem lies in the entity mapping configuration file (xml) of Hibernate.

<id < span style="color: #ff0000;">name="id" type< /span>="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 will automatically generate the strategy automatically according to this id Generate an id.

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

<id < span style="color: #ff0000;">name="id" type< /span>="string" column="id">


id>

This solves the problem, hehe.

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

“I have an unforgettable firefly in my left hand, and a long forgotten ten years in my right.”

< pre><id name< /span>=”id” type=”string” column=”id”>

<generator class=”uuid.hex”/>

id>

<id name="id" type="string" column="id">


id>

Leave a Comment

Your email address will not be published.