29.3.1 实体类
优质
小牛编辑
132浏览
2023-12-01
29.3.1 实体类
习惯上,JPA的“实体”类在persistence.xml
文件中指定。Spring Boot使用“实体扫描”,此文件不是必需的。默认情况下,位于主配置类(一个被@EnableAutoConfiguration
或@SpringBootApplication
注解的类)下的所有包都会被搜索。
任何用@Entity
、@Embeddable
或@MappedSuperclass
注解的类都会被考虑。典型的实体类会像这样:
package com.example.myapp.domain;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class City implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String state;
// ... additional members, often include @OneToMany mappings
protected City() {
// no-args constructor required by JPA spec
// this one is protected since it shouldn't be used directly
}
public City(String name, String state) {
this.name = name;
this.country = country;
}
public String getName() {
return this.name;
}
public String getState() {
return this.state;
}
// ... etc
}
您可以使用
@EntityScan
注解自定义实体扫描位置。请参见第77.4节“从Spring配置中分离@Entity定义”的方法。