5

Framework Joy: Load in Hibernate Updates Data

 2 years ago
source link: https://blog.jakubholy.net/2014/03/31/framework-joy-load-in-hibernate-updates-data/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Framework Joy: Load in Hibernate Updates Data

March 31, 2014
Would you ever guess that this line

// Load Buyer from DB by id using Spring's HibernateTemplate:
final Buyer traveller = (Buyer) hibernateTemplate.load(Buyer .class, new Long(id));


could lead to a constraint-validation exception during a batch update or delete and re-insert rows (loosing all columns Hibernate does not know about)? I was quite surprised.

In particular buyer's bonusCards get deleted and re-inserted, because Hibernate believes that the collection of cards is dirty, even though it has just loaded it from the DB. I am not exactly sure why (preventing false positives in dirty checks requires some black magic), this is the configuration:

<class name="Buyer" ...>
...
    <set name="bonusCards " table="bonus_cards">
        <key column="buyer_id"/>
        <composite-element class="BonusCard">
            <property name="number" column="number" not-null="false"/>
            <property name="expiredate" column="expdate"/>
            ...
        </composite-element>
    </set>
</class>


Of course Hibernate certainly has good reasons to update and delete+re-insert data upon load and we could certainly get rid of (some of) these updates by configuring Hibernate better. But it still demonstrates nicely the hidden cost of using a complex framework - in this case, it behaves quite unexpectedly and requires extensive knowledge to set up properly and to troubleshoot.

Are you benefitting from my writing? Consider buying me a coffee or supporting my work via GitHub Sponsors. Thank you! You can also book me for a mentoring / pair-programming session via Codementor or (cheaper) email.

Allow me to write to you!

Let's get in touch! I will occasionally send you a short email with a few links to interesting stuff I found and with summaries of my new blog posts. Max 1-2 emails per month. I read and answer to all replies.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK