ฤดูใบไม้ผลิบูต JPA ปรับปรุงต้องปรับปรุงเดียวที่เจาะจงช่องข้อมูล

0

คำถาม

ดังนั้นฉันเจอนี่ปัญหากับกำลังปรับปรุงเป็นสิ่งมีชีวิตแบบหนึ่งใน DB. ในขณะที่ผ่านไปทั้งหมดรายการและกำลังปรับปรุงเดียวที่เจาะจงช่องข้อมูลมันกินไม่ได้แตะต้องเลยด้ฟิลด์เป็นโพรโทคอล aimcomment อย่างที่เป็นผลมาจากการพยากรณ์ฉันมีข้อยกเว้นแล้วตั้งแต่พวกนั้นช่องข้อมูลเป็น @Not-Null,

ฉันพยายามตามหาคล้ายๆกับปัญหาแต่ไม่สามารถแก้ไขปัญหาของฉัน

บริษัทรายการ:

@Entity
@Table (name = "companies")
@Data
@ToString(exclude = "perfumes")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotNull
    private String name;

    @NotNull
    @Email(message = "Wrong input. please enter a VALID email address")
    private String email;

    @NotNull
    @Size(min = 4, max = 14, message = "Password range must be between 4 - 14 digits")
    private String password;

    @NotNull
    @Enumerated(EnumType.STRING)
    private Country country;

    @Singular
    @OneToMany(mappedBy = "company", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Perfume> perfumes = new ArrayList<>();
}

ช่องข้อมูลส่วนใหญ่เป็น @NotNull สำหรับการสร้างอย่างไรก็ตาม,ฉันต้องการจะปรับปรุงรายการ,บางครั้งเดียวที่เจาะจงช่องข้อมูล.

บริการ:

@Override
public String updateCompany(int id, Company company) throws DoesNotExistException {
    if(!companyRepository.existsById(id))
    {
        throw new DoesNotExistException(id);
    }

    companyRepository.saveAndFlush(company);
    return company.getName() + " has been UPDATED";
}

อย่างที่คุณสามารถเห็น ENTITY ถูกผ่านไปซึ่งทำให้เหลือของแอททริบิวต์ต่างๆที่จะอัตโนมัติโพรโทคอล aimcomment ถ้าไม่ได้ทำการเปลี่ยนแปลง

Controller:

  @PutMapping("/updateCompany/{id}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public String updateCompany(@PathVariable int id, @RequestBody Company company) throws DoesNotExistException {
        return admin.updateCompany(id,company);
    }

ยกเว้น:

Validation failed for classes [com.golden.scent.beans.Company] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=password, rootBeanClass=class com.golden.scent.beans.Company, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]

ขอบคุณ

hibernate java jpa
2021-11-21 18:38:43
1

คำตอบที่ดีที่สุด

0

ที่ controller คือผูกมัดหรองค่าของคุณผ่านไปในที่ใหม่องค์กรค์. ใหม่ของรายการไม่ยึดติดกับคน persistence คอนเท็กซ์,มันไม่มีทางรัฐของก่อนที่มีอยู่รายการ. เมื่อคุณทำการบันทึกมัน JPA คิดว่าคุณต้องการให้โพรโทคอล aimcomment ออกไปตลอดช่องข้อมูลคุณไม่มีค่าสำหรับ.

แทนที่จะเป็นคุณสามารถมี controller ให้พวกมันมีอาร์กิวเมนต์ที่ DTO. แล้วในบริการคุณค้นหาที่มีอยู่เป็นลูกค้าใช้ findById และคัดลอกที่ช่องข้อมูลคุณต้องการปรับปรุงจาก DTO ไปที่รายการ. งั้นโทรหา saveAndFlush ผ่านไปในที่ปรับปรุงรายการ.

มันดูเหมือนจะมีการพัฒนาทาง DTO คุณสามารถใช้ aJsonPatch จะถือปรับปรุงผ่านเข้ามาเห็น https://www.baeldung.com/spring-rest-json-patch. การรวมกลุ่มกัวิธีการเหมือนจะเป็นดีขึ้นตรงกันกับสิ่งที่นายกำลังพยายามทำอยู่แล้ว

บนเซิร์ฟเวอร์สิ่งที่สำคัญคือการค้นหาที่มีอยู่รายการดังนั้นคุณต้องการค์นั้นคือยึดติดกับคน persistence คอนเท็กซ์และทั้งหมดมันช่องข้อมูลปัจจุบัน.

2021-11-21 20:00:13

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่