简体   繁体   中英

How to load missing object attributes before persisting?

I have a question about Jackson and Hibernate. My application is based on rest and objects are transferred between frontend and backend as json, so I have some situations when some object's attributes are missing when I deserialize json to java object and I'd like to load those attributes before persisting changes (because I don't want to lose that data from database). Has anybody any ideas to solve this problem?

Edit

I am not sure that my question has understood right. So I give simple example, what I try to say.

So I have following Java class:

@Entity
@Table( name = "employees" )
public class Employee extends BaseEntity<Long> {

  private String lastName;
  private String firstName;

  @Embedded 
  private Address address;

  //... a lot of other attributes and methods.. 

}

Now I get json data from frontend, which is something like this:

{
 "id":17,
 "lastName":"Smith",
 "firtName":"John"
}

Next I want to save these changes to database but my deserialized java entity is totally incomplete, there are a lot of missing attributes and references (values are nulls). How can I load those missing attribute values before persisting object, without losing those new values that I got from UI?

I have tried to use EntityManager's merge-method but it didn't work...

从数据库加载数据(如果记录已经存在),请执行合并和保存。

You can map multiple DTOs as JPA @Entities to the same database table. When you save one such DTO, only it's fields are propagated to the DB, without interfering with other database column the current DTO hasn't mapped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM