Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在POST正文中发送数据,而不在Spring Boot中设置自动生成的主键值?_Hibernate_Spring Boot_Post_Postman_Hibernate Mapping - Fatal编程技术网

Hibernate 如何在POST正文中发送数据,而不在Spring Boot中设置自动生成的主键值?

Hibernate 如何在POST正文中发送数据,而不在Spring Boot中设置自动生成的主键值?,hibernate,spring-boot,post,postman,hibernate-mapping,Hibernate,Spring Boot,Post,Postman,Hibernate Mapping,我有两个类/表——Customer和Address具有双向一对一关系。地址\u id是外键 这是实体图 我试图通过postman发送数据,但我希望在不设置post正文中的主键属性的情况下发送值。如果我在中仅为customer省略id属性,那么它是有效的但如果我对地址做同样的操作,则无效。 这是成功插入数据的帖子正文 <Customer> <firstName>Dave</firstName> <lastName>Bautista</last

我有两个类/表——Customer和Address具有双向一对一关系。地址\u id是外键

这是实体图

我试图通过postman发送数据,但我希望在不设置post正文中的主键属性的情况下发送值。如果我在中仅为customer省略id属性,那么它是有效的但如果我对地址做同样的操作,则无效。

这是成功插入数据的帖子正文

<Customer>
<firstName>Dave</firstName>
<lastName>Bautista</lastName>
<gender>M</gender>
<date>2012-01-26T09:00:00.000+0000</date>
<addressdto>
<id>7</id>
<city>BANKURA</city>
<country>WEST BENGAL</country>
</addressdto>
</Customer>
控制台中的错误--

顾客

package com.spring.liquibase.demo.dto;

import java.util.Date;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.sun.xml.txw2.annotation.XmlElement;


@JacksonXmlRootElement(localName = "Customer")
public class CustomerDto {

    private int id;
    private String firstName;
    private String lastName;
    private String gender;
    private Date date;
    private AddressDto addressdto;


    public CustomerDto() {
        super();
    }
..getters and setters
地址

public class AddressDto {

    private int id;
    private String city;
    private String country;


    public AddressDto() {
        super();
    }
EntityToDtoMapper

public Customer mapToEntity(CustomerDto customerDto) {
        Address address=new Address();
        address.setCity(customerDto.getAddressdto().getCity());
        address.setCountry(customerDto.getAddressdto().getCountry());
        address.setId(customerDto.getAddressdto().getId());

        Customer customer=new Customer();
        customer.setId(customerDto.getId());
        customer.setFirstName(customerDto.getFirstName());
        customer.setLastName(customerDto.getLastName());
        customer.setGender(customerDto.getGender());
        customer.setDate(customerDto.getDate());
        customer.setAddress(address);
        return customer;    
    }
家庭控制器

@PostMapping("/customer")
     public ResponseEntity<String> addCustomer(@RequestBody CustomerDto customerDto){
        String message="";
        ResponseEntity<String> finalMessage=null;
        try {

        Customer customer=mapper.mapToEntity(customerDto);
        customerService.addCustomer(customer);
        message="Customer with "+customer.getId()+" sucessfully added";
        finalMessage= new ResponseEntity<>(message, HttpStatus.OK);

    }catch(Exception e) {
        message="Failed to add customer due to "+e.getMessage();
        finalMessage= new ResponseEntity<>(message, HttpStatus.NOT_ACCEPTABLE);
    }
        return finalMessage;
    }
客户实体

@Entity
@Table(name="customers")
public class Customer {

    @Id
    private int id;
    @Column(name="first_name")
    private String firstName;
    @Column(name="last_name")
    private String lastName;
    private String gender;

    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="address_id")
    private Address address;

    public Customer() {
        super();
    }
...getters an setters

@GeneratedValue
添加到id字段以正确自动生成实体id。请换

@Id
private int id;

在两个实体上

然后在
mapToEntity
方法中添加以下行:


address.setCustomer(客户)

通过添加一个新的字段变量作为
addressId

@Entity
@Table(name="customers")
public class Customer {

    @Id
    private int id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    private String gender;

    @Column(name="address_id")
    private int addressId;

    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="address_id")
    private Address address;

    public Customer() {
        super();
    }
}
并更改EntityDTOMapper,如下所示

public Customer mapToEntity(CustomerDto customerDto) {
    Address address=new Address();
    address.setCity(customerDto.getAddressdto().getCity());
    address.setCountry(customerDto.getAddressdto().getCountry());
    address.setId(customerDto.getAddressdto().getId());

    Customer customer=new Customer();
    customer.setId(customerDto.getId());
    customer.setFirstName(customerDto.getFirstName());
    customer.setLastName(customerDto.getLastName());
    customer.setGender(customerDto.getGender());
    customer.setDate(customerDto.getDate());

    customer.setAddressId(customerDto.getAddressdto().getId());
    //customer.setAddress(address);

    return customer;    
}

是否仅针对地址id 7插入客户,或同时插入客户和地址?否,我要插入一个具有地址的新客户条目。但我不想在帖子正文中指定自动生成的id列。我认为您需要将您的客户更改为具有address\u id字段。在准备仅设置客户地址时,请尝试保留客户。客户是否也是JPA实体?你能发布客户分类代码吗?我已经添加了实体分类,请告诉我如何解决问题。我还不能解决它。或者这是它的工作方式,如果两个类具有
has-a
关系,那么我们只能在
Customer
类的post主体中省略自动生成的主键,该类具有
地址
。我们必须在PostBodyAddress中强制设置地址的id列。地址没有客户。客户有自己的地址。请看一下DTO。那么它不是双向绑定。你可以发布客户和地址实体类的代码吗?不,这是一个双向绑定,我已经添加了实体类,请看一下。完美的解决方案!
@Id
private int id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
@Entity
@Table(name="customers")
public class Customer {

    @Id
    private int id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    private String gender;

    @Column(name="address_id")
    private int addressId;

    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="address_id")
    private Address address;

    public Customer() {
        super();
    }
}
public Customer mapToEntity(CustomerDto customerDto) {
    Address address=new Address();
    address.setCity(customerDto.getAddressdto().getCity());
    address.setCountry(customerDto.getAddressdto().getCountry());
    address.setId(customerDto.getAddressdto().getId());

    Customer customer=new Customer();
    customer.setId(customerDto.getId());
    customer.setFirstName(customerDto.getFirstName());
    customer.setLastName(customerDto.getLastName());
    customer.setGender(customerDto.getGender());
    customer.setDate(customerDto.getDate());

    customer.setAddressId(customerDto.getAddressdto().getId());
    //customer.setAddress(address);

    return customer;    
}