One to One Mapping in Springboot Jpa

 

One to One Mappin in Springboot models


One to One relation b/w customer and order means one customer can place only one order at a time First Model(table)






Customer
@Entity
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
public class Customer {
@Id
@GeneratedValue
private int id;
private String customerName;
private String customerContact;

@OneToOne(mappedBy = "customer", cascade = CascadeType.ALL)
private Order order;
public Customer(String customerName, String customerContact){
this.customerContact = customerContact;
this.customerName = customerName;
}
}
Order
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue
private int id;
private double total;
private String orderType;
private Date orderDate;
@OneToOne
@MapsId
private Customer customer;
public Order(double total, String orderType, Date orderDate) {
this.total = total;
this.orderType = orderType;
this.orderDate = orderDate;
}

}

Comments

Popular Posts