假设我有以下实体:
@Entity
@Table (name = "customers)
public class CustomerImpl implements Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "surname", nullable = false)
private String surname;
@Column(name = "country", nullable = false)
private String country;
@Column(name = "city", nullable = false)
private String city;
@Column(name = "street", nullable = false)
private String street;
@Column(name = "zipCode", nullable = false)
private String zipCode;
// constructor + getters
}
我想创建一个名为地址的新类,它将包含国家,街道,城市和邮政编码变量。然后,概述的变量将替换为Address对象,以使代码更易读和更清晰。
在我的情况下,应该嵌入Address类还是应该将其指定为新实体并在Address和Customer之间给出@OneToOne关系?
谢谢
答案 0 :(得分:1)
为了使您的代码更具可读性和清晰度,您应该创建一个包含country,city ...的ADRESSE表,然后您可以使用@OneToOne关系,如下所示:
在Customer类中:
@OneToOne(mappedBy = "customer", fetch = FetchType.LAZY)
private Address address;
// Getter + Setter
在Adresse课程中:
@OneToOne
@JoinColumn(name="id")
private Customer customer;