【发布时间】:2015-12-22 20:07:53
【问题描述】:
传统上,JPA“实体”类在 persistence.xml 文件中指定。对于 Spring Boot,这个文件不是必需的,而是使用“实体扫描”
@Entity
public class Hotel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@ManyToOne(optional = false)
@NaturalId
private City city;
@Column(nullable = false)
@NaturalId
private String name;
@Column(nullable = false)
private String address;
@Column(nullable = false)
private String zip;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "hotel")
private Set<Review> reviews;
protected Hotel() {
}
public Hotel(City city, String name) {
this.city = city;
this.name = name;
}
public City getCity() {
return this.city;
}
public String getName() {
return this.name;
}
public String getAddress() {
return this.address;
}
public String getZip() {
return this.zip;
}
}
如何通过工具创建 Spring Boot 实体?我不想写实体手册。
【问题讨论】:
-
您可以使用 Lombok 项目 projectlombok.org/features/GetterSetter.html 中的
@Getter和@Setter(以及更多功能)释放大量代码 -
没有像 Spring Boot Entity 这样的东西,它只是一个普通的 JPA 实体,就像任何其他实体一样......而且传统上你不必在persistence.xml 你也可以让 JPA 扫描它们(这也是 spring 在配置 EntityManager 时使用的)。所以默认情况下 Spring (Boot) 所做的一切都已经存在(但更容易)。
标签: spring-boot spring-data-jpa