In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.