简体   繁体   English

hibernate 和 spring 中的持久性

[英]Persistence in hibernate and spring

I have a question about Spring and Hibernate and that is, if for example I want to persist two entities at the same time, such as Customer and Address, that when a customer is registered, their address is automatically saved as well.我有一个关于 Spring 和 Hibernate 的问题,例如,如果我想同时保留两个实体,例如客户和地址,当客户注册时,他们的地址也会自动保存。

It is that the address is retrieved from an address api external to my application and I want that information to be saved in the Address entity when the client registers.这是从我的应用程序外部的地址 api 检索到的地址,我希望在客户端注册时将该信息保存在地址实体中。

My entities are the following:我的实体如下:

Customer:顾客:

public class Customer{
     
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id;

    @NotBlank(message = "no puede estar vació")
    private String nombre;

    @NotBlank(message = "no puede estar vació")
    @Column(name = "apellido_paterno")
    private String apellidoPaterno;

    @NotBlank(message = "no puede estar vació")
    @Column(name = "apellido_materno")
    private String apellidoMaterno;
    
    @NotBlank(message = "no puede estar vació")
    private String telefono;
 
 
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "address_id")
    @JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
    private Address address;
}

Address:地址:

public class Address{

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id;
 
    @NotBlank(message = "no puede estar vació")         
    private String calle;
    
    @NotBlank(message = "no puede estar vació") 
    private String colonia;
    
    @NotNull(message = "no puede estar vació")
    @Column(name = "no_exterior")   
    private Integer noExterior; 
 
    @Column(name = "no_interior")   
    private Integer noInterior;
 
    @NotBlank(message = "no puede estar vació") 
    private String municipio;
    
    @NotNull(message = "no puede estar vació")
    private Integer cp; 
    
     @NotBlank(message = "no puede estar vació")    
    private String estado;
 
    @OneToMany(mappedBy = "adress")
    private List<Customer> customer;
}

Since I have it, the address must already be created so that it can be associated with the client, however, I want that when the client enters their data, including the address, they are created at the same time and the association is maintained.既然我有它,地址必须已经创建,以便它可以与客户端关联,但是,我希望当客户端输入他们的数据(包括地址)时,它们会同时创建并保持关联。

When you persist the Entity "Customer" you should create a object Customer and a object Address, and set to the Customer the Address object and add to the Customer list in Address object, the object Customer. When you persist the Entity "Customer" you should create a object Customer and a object Address, and set to the Customer the Address object and add to the Customer list in Address object, the object Customer. I mean, you should do it in your business logic layer, or your service.我的意思是,您应该在业务逻辑层或服务中执行此操作。

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

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