In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about the JPAsave () method to update the field to null solution, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.
Today, I encountered a problem in development. During the page display, it was passed to the front end ten fields, and the front end modified the values of 3 of them, so only 3 values were passed to the backend, and the rest of the fields defaulted to null. After the update, all the other 7 fields have become null.
Under the premise that the front end cannot send back all the attributes, the back end will deal with this kind of problem. Solution:
1. Write a tool method (UpdateUtil) to filter out all null fields
two。 When updating, search the original object through id, findone (), etc.
3. Overwrite the original object attribute value with the non-empty parameter (that is, the value to be modified) copy passed from the front end, through BeanUtils.copyNullProperties (Object source, Object target)
Here are the tool methods:
/ * Utility class (ignore fields with null) * / public class UpdateUtil {/ * all properties with null values are not copy * * @ param source * @ param target * / public static void copyNullProperties (Object source, Object target) {BeanUtils.copyProperties (source, target, getNullField (source)) } / * get the empty field in the attribute * * @ param target * @ return * / private static String [] getNullField (Object target) {BeanWrapper beanWrapper = new BeanWrapperImpl (target); PropertyDescriptor [] propertyDescriptors = beanWrapper.getPropertyDescriptors (); Set notNullFieldSet = new HashSet (); if (propertyDescriptors.length > 0) {for (PropertyDescriptor p: propertyDescriptors) {String name = p.getName (); Object value = beanWrapper.getPropertyValue (name); if (Objects.isNull (value)) {notNullFieldSet.add (name) } String [] notNullField = new String [notNullFieldSet.size ()]; return notNullFieldSet.toArray (notNullField);}}
The above is the solution of the JPAsave () method to update the field to null. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.