简体   繁体   English

从一个 object 复制 not null and not empty fields 到 java 中的另一个 object 相同类型(对象是相同类型)

[英]Copy not null and not empty fields from one object to another object of the same type(Objects are same type) in java

I am using hibernate, struts, extjs in my project.我在我的项目中使用hibernate,struts,extjs。

I have a Customer object with these fields:我有一个具有以下字段的客户 object:

custId,客户编号,
custName, address, phone客户姓名、地址、电话

and in my project from UI side I get an object customer with custName .在我的 UI 项目中,我得到了一个 object 的custName客户。

So I need to update the above object( custName is unique).所以我需要更新上面的对象( custName是唯一的)。 I have only one object with the same customer name.我只有一个 object 具有相同的客户名称。 So I will get that object using customer name (object from DB).所以我将使用客户名称(来自数据库的对象)获得 object。

Now I have to save the object with the updated customer name.现在我必须用更新后的客户名称保存 object。

If I save as follows I have Customer Object from UI, is cust如果我按如下方式保存,我有来自 UI 的客户 Object,是客户

Customer cust1 = getCustomerByName(cust.getCustName());客户 cust1 = getCustomerByName(cust.getCustName());

cust.setCustId(cust1.getCustId()); cust.setCustId(cust1.getCustId()); save(cust);保存(客户);

If I do this I lose the customer address and phone number.如果我这样做,我会丢失客户地址和电话号码。

So, how can I copy one object not null or not empty field values to another object of same type.那么,如何将一个 object 而不是 null 或非空字段值复制到另一个相同类型的 object。

Can any one please help.任何人都可以帮忙。 I just stuck here.我只是被困在这里。 It's stopping me to save.它阻止我保存。

Thanks in advance!提前致谢!

You are mixing up your references to cust and cust1 .您混淆了对custcust1的引用。

Unless cust already had an address, no amount of setting JUST the custId on cust would also copy the address from cust1 .除非cust已经有一个地址,否则在cust上仅设置custId也不会从cust1复制地址。

Two options.两种选择。 Use reflection to find all fields and copy the ones that are not null. Or better option, write an update method that explicitly checks each field and updates the database instance's fields as appropriate.使用反射查找所有字段并复制不是 null 的字段。或者更好的选择是编写一个更新方法来显式检查每个字段并根据需要更新数据库实例的字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-对象中具有相同数据类型的多个字段 - Java - Multiple fields of same data type in an object 如何将一个对象字段数据复制到同一类的另一对象? - How it copy one object fields data to another object of the same class? 如何在Java中将对象从一种类型复制到另一种类型? - How to copy an object from one type to another in Java? 如何比较一个对象与另一个对象是否具有相同的类型 - How to compare if one object is the same type as another 在REST中使用相同类型的另一个对象更新对象的最佳方法 - Best way to update an object with another object of the same type in java in rest 是否有一种“干净”的方法可以将 Java 中的 object 的所有属性值替换为来自同一类型的另一个 object 的值? - Is there a "clean" way to replace all attribute values of an object in Java with values from another object of the same type? 从一个列表中提取多个字段并将其保存到另一列表或相同类型的新列表中。 在Java 8中 - Extracting multiple fields from one list and save it to another list or same type new list. In java 8 同一类型的多个对象如何同时与另一种类型的 object 一起工作? - How can several objects of the same type work with an object of another type at the same time? 如何从流中收集X类型对象,该流仅包含同一X类型对象的某些字段,而不使用辅助收集 - How to collect X type objects from a stream, that contain only some fields of the same X type object, without using an auxiliary collection 从具有相同类型的多个字段的字符串构造到嵌套对象 - Mapstruct from string to nested object for multiple fields with the same type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM