java - How to load a persisted Hibernate entity for binding so non-form properties are not lost? -


I am using Spring 4 and Hibernate 4. I have a problem that seems like many other people I want to edit my spring form only a few special properties of an entity, not the whole thing.

By default, if I load a permanent unit, then edit some of it in some form to show some properties, post it to my controller and save it ... any property The values ​​which were not included in the form are lost, I think why this happens, and I know of some solutions that float around there.

1) On saving time, load the unit with DB, only copy the properties you want, save the model, save.

2) When the form is started, then load the unit and use the @Session entries feature to store it in sessions between requests.

But I am not like either one of these. Maybe I'm missing something, but the solution seems quite simple. Somewhere, the default spring web binder is making an example of the unit to copy the property's form. If I can override it and I can use the ID to load a permanent unit from DB, then the use of the spring using the spring is in the form of a target unit (by copying the assets presented with the form, Leaving others untouched) ... then I think that will solve my problem.

I see how I can use a custom webdirector class. I do not know how .... and I do not even know if that place should show me.

Any suggestions?

Thank you!

Edit:
I give a fictional example to try to understand my problem better. Suppose I have an entity class, customer, which has 50 properties. I only have one form to update the contact information, this is only 5 out of 50.

Street
City
State
Zip
Phone

If I do this in a spring form, when saving the form The spring makes a new 'empty' unit, fills it with the values ​​from the form and transmits it to my controller ... this is what is saved, the problem is because the other properties such as The first names, last names, customer types, etc. were not included in the form, spring them alone Leaves and they are zero. When I tell Hibernate that to update the unit using this provided by Spring, I believe in hibernation that I want to be nullified first, name, last name, because they are those values ​​which I am providing in Bean. It does not know that I have changed some of them.
What happens then? 45 of my 50 property values ​​are blown away.

In the comments below, Sp00m suggested a solution that will work ... Include all other properties in the hidden areas but for many reasons, this solution is a major headache for me.

If I can get the Spring to load the database from the spring (instead of creating a new / empty unit) in the unit and then those areas were presented with that form I believe That would solve my problem.

Similar discussions (but with hidden field solutions) can be found here.

is the solution that works for me, by adding the following method to the controller in the controller, the spring uses it to instantify the unit for binding. Actually what I want

(I actually tried it in advance, but in the comment I did not specify the name of the model attribute, which is clearly not good.)

Thanks to all those people who commented with the suggestion. @ModelAttribute ("unit") Public MyEntity getEntity (@RevestParam (required = false) integer ID) {if (id == null) {myEntityManager.createEntity (entityClass); } And {myEntityManager.find (entity class, id); }}


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -