Como puedo hacer para que se guarden los datos relacionados?se guardan en la bd pero no relacionados
Metodo en CustomerController.java
@PostMapping("/customer")
public Customer postCustomer(@RequestBody Customer customer) {
User user= new User(customer.getUser().getUserName(),
customer.getUser().getPassword()
,customer.getUser().getEmail());
Customer _customer = new Customer();
User _user = new User();
_customer.setUser(_user);
_user.setCustomer(_customer);
_user = usuarioRepository.save(new User(user.getUserName(),user.getPassword(),user.getEmail()));
_customer = customerRepository.save(new Customer(customer.getName(),customer.getAge()));
return _customer;
}
user.java
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private long id;
@Column(name="userName")
private String userName;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
@Column(name="logged")
private boolean logged;
@OneToOne
@JoinColumn(name = "customer_id")
private Customer customer;
public User() {
}
public User(String userName,String password,String email) {
this.userName=userName;
this.password=password;
this.email=email;
}
customer.java
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@OneToOne(mappedBy = "customer")
private User user;
public Customer() {
}
public Customer(String name, int age) {
this.name = name;
this.age = age;
}