-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Unchanged fields are nulled
PostPosted: Fri Mar 03, 2017 12:37 pm 
Newbie

Joined: Fri Mar 03, 2017 11:28 am
Posts: 3
I'll quote the first bit of my stackoverflow post here, to get the context right. After I have posted this I've found something I believe is not right:

"I've an Spring Boot app which, of course, has entities. These entities get updated via an thymeleaf form. In this form only some relevant fields are changeable. For example the name of the entity. Other fields like, created, createdby or lastUsedBy are not controlled / changed by this form.

Now heres the problem: If we now change the entity, all other fields are set to null, because they are not in the request. One approach would be to add for these missing fields <input type="hidden"/>inputs. But this not very elegant and error prone. Than I've come to the conclusion, that hibernate should only update those fields which have been changed. So this has to be done via DirtyTracking. I've currently another app, which uses OpenJPA with the openJPA Enhancer and in this app the update only updates the changed fields. My assumption was that the Hibernate enhancer would solve my issue. But even with dirty tracking enabled all fields are updated and information gets lost. I've managed to get it working when I add the @DynamicUpdate annotation to the given entity, but this can't be the right way right?"

So. I've tested a bit further, even without the bytecode enhancing for dirty tracking and found that the setPropertyValues method in the AbstractEntityTuplizer gets an object array of changed values. I suspect that this method should only call the setters for those changed values but instead it calls every setter of the entity and sets the fields to null if they are not in the values parameter array.

Am I missing something here or how does this properly work?

Stackoverflow post: http://stackoverflow.com/questions/4258 ... d-tracking


Top
 Profile  
 
 Post subject: Re: Unchanged fields are nulled
PostPosted: Fri Mar 03, 2017 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Check out my answer on StackOverflow.


Top
 Profile  
 
 Post subject: Re: Unchanged fields are nulled
PostPosted: Fri Mar 03, 2017 3:01 pm 
Newbie

Joined: Fri Mar 03, 2017 11:28 am
Posts: 3
Thanks for your reply on StackOverflow.

But as I mentioned there, those are just workarounds in my eyes. If it works with OpenJPA without those workaround why can't this work also with Hibernate?

Why does Hibernate provide this values Object, which holds ONLY the changed values but sets all other values on the entity regardless?

Code:
public void setPropertyValues(Object entity, Object[] values) throws HibernateException {
    boolean setAll = !entityMetamodel.hasLazyProperties();

    for ( int j = 0; j < entityMetamodel.getPropertySpan(); j++ ) {
        if ( setAll || values[j] != LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
            setters[j].set( entity, values[j], getFactory() );
        }
    }
}


Top
 Profile  
 
 Post subject: Re: Unchanged fields are nulled
PostPosted: Fri Mar 03, 2017 3:27 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Those arrays are called the loadedState, in Hibernate terminology, and they are used for two things:

1. For the default dirty checking mechanism
2. For the second-level cache

JPA only states what a provider should do, not how it should do it, and, as you'll soon find out, portability is just a myth. In reality, each JPA provider does things in its own way, so, in the end, you have to know JPA provider-specific details to work effectively.


Top
 Profile  
 
 Post subject: Re: Unchanged fields are nulled
PostPosted: Fri Mar 03, 2017 3:36 pm 
Newbie

Joined: Fri Mar 03, 2017 11:28 am
Posts: 3
Yeah.. Portability. Already found out the hard way that this an myth. Tried to migrate an old OpenJPA project to Hibernate and failed after over an year miserable...

I get, that those arrays are used for dirty checking but what I do not get is that they are provided to this function but without any use. Would be any disadvantage to only call the setters for the values provided?

But either way, it seems like I am stuck with saving the entity in the session or mapping those changed values by myself.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.