gpt4 book ai didi

Spring Boot Update(Spring Boot更新)

转载 作者:bug小助手 更新时间:2023-10-25 18:09:30 30 4
gpt4 key购买 nike



I have spring boot application and i'm working on update request so my enitity is so big and have nested object and classes so how can i update this entity with more professtional way so i dont have to check on every properties if null or empty

我有SpringBoot应用程序,我正在处理更新请求,所以我的enitity如此之大,并且有嵌套的对象和类,所以我如何才能用更专业的方式更新这个实体,这样我就不必检查每个属性是否为空


I tried to check with if else condtions but it so complex and the code will be so bad

我试着检查是否有其他条件,但它太复杂了,代码也会很糟糕


special when I have getLevelOfConsciousness have multi properties to check

当我有多个要检查的属性时,特别是当我有了getLevelOfConsights


boolean changes = false;
if(update.getLevelOfConsciousness() != null && !update.getLevelOfConsciousness().equals(existsChart.getLevelOfConsciousness())) {
existsChart.setLevelOfConsciousness(update.getLevelOfConsciousness());
changes = true;
}if(update.getOrientation() != null && !update.getOrientation().equals(existsChart.getOrientation())){
existsChart.setOrientation(update.getOrientation());
changes = true;
}if(update.getMentalState() != null && !update.getMentalState().equals(existsChart.getMentalState())){
existsChart.setMentalState(update.getMentalState());
changes = true;
}if(update.getEyes() != null && !update.getEyes().equals(existsChart.getEyes())){
existsChart.setEyes(update.getEyes());
changes = true;
}if(update.getEars() != null && !update.getEars().equals(existsChart.getEars())){
existsChart.setEars(update.getEars());
changes = true;
}if(update.getMouth() != null && !update.getMouth().equals(existsChart.getMouth())){
existsChart.setMouth(update.getMouth());
changes = true;
}if(update.getNose() != null && !update.getNose().equals(existsChart.getNose())){
existsChart.setNose(update.getNose());
changes = true;
}if(update.getHair() != null && !update.getHair().equals(existsChart.getHair())){
existsChart.setHair(update.getHair());
changes = true;
}if(update.getNeck() != null && !update.getNeck().equals(existsChart.getNeck())){
existsChart.setNeck(update.getNeck());
changes = true;
}if(update.getSkin() != null && !update.getSkin().equals(existsChart.getSkin())){
existsChart.setSkin(update.getSkin());
changes = true;
}if(update.getChest() != null && !update.getChest().equals(existsChart.getChest())){
existsChart.setChest(update.getChest());
changes = true;
}if(update.getAbdomen() != null && !update.getAbdomen().equals(existsChart.getAbdomen())){
existsChart.setAbdomen(update.getAbdomen());
changes = true;
}if(update.getUpperExtremities() != null && !update.getUpperExtremities().equals(existsChart.getUpperExtremities())){
existsChart.setUpperExtremities(update.getUpperExtremities());
changes = true;
}if(update.getLowerExtremities() != null && !update.getLowerExtremities().equals(existsChart.getLowerExtremities())){
existsChart.setLowerExtremities(update.getLowerExtremities());
changes = true;
}if(update.getVitalSign() != null && !update.getVitalSign().equals(existsChart.getVitalSign())){
existsChart.setVitalSign(update.getVitalSign());
changes = true;
}if(update.getNote() != null && !update.getNote().equals(existsChart.getNote())){
existsChart.setNote(update.getNote());
changes = true;
}
if (!changes){
throw new BadRequestException("No changes found");
}

更多回答
优秀答案推荐

You can use Hibernate @DynamicUpdate annotation with the entity. In this case, hibernate will fire an update query only for the field that we want to update.

您可以对实体使用Hibernate @DynamicUpdate注释。在这种情况下,hibernate将只对我们想要更新的字段触发更新查询。


@Entity
@DynamicUpdate
public class MyEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "name")
private String name;
//
}

You can check more details about @DynamicUpdate example here.

您可以在此处查看有关@DynamicUpdate示例的更多详细信息。


If you want to get stuck with your approach you might consider using reflection to compare the values.
Here is the sample code.

如果您想坚持您的方法,您可以考虑使用反射来比较这些值。以下是示例代码。


@Transactional
public Student save(Student student) {
Student createResponse = null;
createResponse = studentRepository.save(student);
return createResponse;
}

@Transactional
public Student update(Student student) {

Optional<Student> response = studentRepository.findById(student.getId());
Student fromDb = response.get();
boolean changes = false;
if (fromDb != null) {

// Use reflection to get and compare the values of all fields
Field[] fields = Student.class.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
Object existingValue = field.get(fromDb);
Object entityNeedsToUpdate = field.get(student);

if (!Objects.equals(existingValue, entityNeedsToUpdate)) {
field.set(fromDb, entityNeedsToUpdate);
changes = true;

}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
if (changes) {
return studentRepository.save(fromDb);
}else{
return student;
}


}
return student;
}


Looks like you need to check if there were any change between previous object and current object.
You may first convert objects to json and compare.

看起来您需要检查前一个对象和当前对象之间是否有任何更改。您可以首先将对象转换为json并进行比较。


public static boolean compare(Object obj1, Object obj2) {
return new Gson().toJson(obj1).equals(new Gson().toJson(obj2));
}
compare(update,existsChart);

Or you may use Java Map to check for changes :

或者,您可以使用Java Map检查更改:


ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> aMap = objectMapper.convertValue(update, Map.class);
Map<String, Object> bMap = objectMapper.convertValue(existsChart, Map.class);

aMap.equals(bMap);


更多回答

first of all thank you for help. i have tried this before but it not the right way and it will cose so meny issue so asked here to fine another way. thank you again

首先,感谢您的帮助。我以前试过了,但它不是正确的方式,它会引起如此大的问题,所以请在这里罚款另一种方式。再次感谢您

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com