From 1ef21c47670e26e4b3a1aee2bc1f931a5a6386a2 Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Thu, 12 Feb 2015 10:19:20 +0800 Subject: [PATCH] Update 28.3.1. Entity Classes.md --- .../28.3.1. Entity Classes.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/IV. Spring Boot features/28.3.1. Entity Classes.md b/IV. Spring Boot features/28.3.1. Entity Classes.md index e69de29..9969959 100644 --- a/IV. Spring Boot features/28.3.1. Entity Classes.md +++ b/IV. Spring Boot features/28.3.1. Entity Classes.md @@ -0,0 +1,47 @@ +### 28.3.1. 实体类 + +传统上,JPA实体类被定义到一个persistence.xml文件中。在Spring Boot中,这个文件不是必需的,并被'实体扫描'替代。默认情况下,在你主(main)配置类(被@EnableAutoConfiguration或@SpringBootApplication注解的类)下的所有包都将被查找。 + +任何被@Entity,@Embeddable或@MappedSuperclass注解的类都将被考虑。一个普通的实体类看起来像下面这样: +```java +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注解自定义实体扫描路径。具体参考[Section 67.4, “Separate @Entity definitions from Spring configuration”](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-separate-entity-definitions-from-spring-configuration)。