3 Metadata
通过javax.persistence 包中定义的Annotation或者XML mapping files来指定Persistence metadata。当混合使用Annotation 和XML mapping file 的时候,如果发生冲突,那么以XML mapping file为准。
3.1 Class Metadata
3.1.1 Entity
Entity annotation用来指定entity class。它有一个属性name用来在Query中引用entity。如果不指定name,那么缺省是entity class的名字(unqualified)。
以下是个使用Entity annotation的例子:
@Entity public class Company { @Id private int id; @Basic private String name; } @Entity public class Address { @Basic private String province; @Basic private String city; @Basic private String street; @Basic private String zip; }
如果使用MySQL数据库,那么用MappingTool生成的SQL如下:
CREATE TABLE Company (id INTEGER NOT NULL, name VARCHAR(255), PRIMARY KEY (id)) TYPE = innodb; CREATE TABLE Address (id BIGINT NOT NULL, city VARCHAR(255), province VARCHAR(255), street VARCHAR(255), zip VARCHAR(255), PRIMARY KEY (id)) TYPE = innodb; CREATE TABLE OPENJPA_SEQUENCE_TABLE (ID TINYINT NOT NULL, SEQUENCE_VALUE BIGINT, PRIMARY KEY (ID)) TYPE = innodb;
JPA要求每个persistent class都必须至少声明一个identity字段,但是OpenJPA允许不声明identity字段。如果persistent class没有声明identity字段,那么OpenJPA会缺省增加id列作为数据库表的主键,同时采用Table Generator的方式为id赋值,Table Generator缺省使用OPENJPA_SEQUENCE_TABLE。
3.1.2 IdClass
当某个entity class中包含多个identity 字段的时候,必须使用identity class(例如之前提到的MagazineId类)。IdClass annotation用来指定identity class,它的value属性是java.lang.Class 型。
3.1.3 MappedSuperclass
Mapped superclass不是entity class,但是它可以为entity class定义persistent state和mapping information。Mapped superclass通常是抽象类。Mapped superclass不能用于Query,或者被运用到任何EntityManager或者Query中。Mapped superclass通过 MappedSuperclass annotation指定。
以下是个使用MappedSuperclass annotation的例子:
@MappedSuperclass public abstract class Document { @Id protected int id; @Version protected int version; } @Entity public class Contract extends Document { @Basic private String name; }
如果使用MySQL数据库,那么用MappingTool生成的SQL如下:
CREATE TABLE Contract (id INTEGER NOT NULL, name VARCHAR(255), version INTEGER, PRIMARY KEY (id)) TYPE = innodb;
3.1.4 Embeddable
Embeddable annotation指定embeddable persistent class。Embeddable instances被映射到datastore record的一部分。JPA要求Persistent class只能是entity class或embeddable class之一。OpenJPA允许Persistent class既是entity class,也是embeddable class。只要同时使用@Entity和@ Embeddable即可。
以下是个使用Embeddable annotation的例子:
@Entity public class Company { @Id private int id; @Basic private String name; @Embedded private Address address; } @Embeddable public class Address { @Basic private String province; @Basic private String city; @Basic private String street; @Basic private String zip; }
如果使用MySQL数据库,那么用MappingTool生成的SQL如下:
CREATE TABLE Company (id INTEGER NOT NULL, name VARCHAR(255), city VARCHAR(255), province VARCHAR(255), street VARCHAR(255), zip VARCHAR(255), PRIMARY KEY (id)) TYPE = innodb;
从以上SQL可以看出,OpenJPA并没有为Address embeddable class生成一个表,而是在Company表中包含了Address信息。
3.1.5 EntityListeners
EntityListeners annotation用来指定entity listeners,它的value属性是java.lang.Class数组型。
3.1.6 Table
Table annotation 用来指定entity class对应的数据库表名。它有以下几个属性:
以下是个简单的例子:
@Entity @Table(name="ART", uniqueConstraints=@Unique(columnNames="TITLE")) public class Article { ... }
3.1.7 SecondaryTable
有时候一个逻辑上的记录被分散在多个数据库表中。通过SecondaryTable annotation可以指定entity class的secondary tables。通过column annotation的table属性来引用这些secondary tables。例如:
@Entity @Table(name="ART") @SecondaryTable(name="ART_DATA", pkJoinColumns=@PrimaryKeyJoinColumn(name="ART_ID", referencedColumnName="ID")) public class Article { @Id private long id; @Column(table="ART_DATA") private byte[] content; ... }
3.2 Field and Property Metadata
JPA提供了两种访问persistent state的方法:field access和property access。每个entity只能使用field access或property access之一来访问同一个persistence state,子类和父类必须使用相同的访问方式来访问同一个persistence state。
如果使用field access,那么JPA会直接把persistent state注入到filed中,并直接从filed上得到persistent state的改变。Annotation直接标记在field上,例如:
@ManyToOne private Company publisher;
如果使用property access,那么JPA会通过Java Bean的"getter" 和 "setter" 方法访问persistent state。Annotation标记在"getter"方法上,例如:
@ManyToOne private Company getPublisher() { ... } private void setPublisher(Company publisher) { ... }
当使用property access 的时候,你应该只通过"getter"和"setter"方法访问persistence state。这个类中其它的方法(例如业务逻辑方法),也应该只通过"getter"和"setter"方法来访问persistence state。当你需要在"getter"和"setter"方法内添加其它业务逻辑的时候,你应该认识到,"getter"和"setter"方法也会被JPA调用。
3.2.1 Transient
Transient annotation指定一个字段为non-persistent。
3.2.2 Id
Id annotation用来指定identity字段。
3.2.3 Generated Value
GeneratedValue 用来为identity字段生成唯一值,它有两个属性,分别是strategy和generator。
Strategy有以下可选值:
JPA支持以下两种generator,分别是Sequence Generator和Table Generator。Sequence Generator有以下属性:
Table Generator有以下属性:
除了identity字段之外,OpenJPA 支持在任何字段上使用 GeneratedValue annotation。 OpenJPA额外提供了两种generator,如下:
以下是使用GeneratedValue的一个例子:
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.TableGenerator; @Entity public class GenerationStrategy { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int identity; @GeneratedValue(strategy = GenerationType.AUTO, generator="uuid-string") private String uuidString; @GeneratedValue(strategy = GenerationType.AUTO, generator="uuid-hex") private String uuidHex; @GeneratedValue(strategy = GenerationType.TABLE, generator="tg1") @TableGenerator(name="tg1", table="GenerationStrategyTable", pkColumnName="id", valueColumnName="sequence", allocationSize=1) private long tableGeneratorValue; }
以上例子中,声明了一个named generator:"tg1"。在运行时可以通过OpenJPAEntityManager. getNamedGenerator("tg1")方法来访问它。笔者通过MappingTool生成以上例子相关SQL的代码如下(也可以通过命令行生成),如果generateSql是false,那么MappingTool会直接更改数据库。
boolean generateSql = true; String java = "./src/com/example/jpa/GenerationStrategy.java"; if(!generateSql) { MappingTool.main(new String[]{java}); System.out.println("the table successfully generated"); } else { String sql = java.replace("java", "sql"); MappingTool.main(new String[]{"-sql", sql, java}); System.out.println("the sql file was successfully generated: " + sql); }
如果使用MySql数据库,那么用MappingTool生成的SQL如下:
CREATE TABLE GenerationStrategy (identity INTEGER NOT NULL AUTO_INCREMENT, tableGeneratorValue BIGINT, uuidHex VARCHAR(255), uuidString VARCHAR(255), PRIMARY KEY (identity)) TYPE = innodb; CREATE TABLE GenerationStrategyTable (ID VARCHAR(255) NOT NULL, SEQUENCE BIGINT, PRIMARY KEY (ID)) TYPE = innodb;
3.2.4 Embedded Id
如果一个entity class有多个identity字段,那么你可以声明多个@Id 字段,或者声明一个@EmbeddedId字段。@EmbeddedId字段必须是一个embeddable entity class。
3.2.5 Version
Version annotation用来指定version字段。
3.2.6 Basic
跟Transient annotation不同,basic annotation指定一个字段为persistent。可以在以下类型的字段上使用basic annotation:primitives, primitive wrappers, java.lang.String, byte[], Byte[], char[], Character[], java.math.BigDecimal, java.math.BigInteger, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Timestamp, Enums, 和 Serializable types.
Basic annotation有以下两个属性:
OpenJPA 可以在任何字段上进行延迟加载(lazy-load),也允许在运行时动态修改某个字段的加载方式。
3.2.7 Embedded
@Embedded字段必须是字段必须是一个embeddable entity class,它用来映射datastore record的一部分。
3.2.8 Many To One
如果多个entity A引用entity B,而且其它entity A也可能引用相同的entity B,那么称A和B之间的关系是Many To One。JPA使用ManyToOne annotation来指定这种关系。它有以下属性:
关于Cascade Type,有以下可选值:CascadeType.PERSIST、CascadeType.REMOVE、CascadeType.REFRESH和CascadeType.MERGE。CascadeType.ALL用来指定以上所有CascadeType,以下两种写法是等价的:
@ManyToOne(cascade={CascadeType.PERSIST,CascadeType.REMOVE, CascadeType.REFRESH,CascadeType.MERGE}) private Company publisher; @ManyToOne(cascade=CascadeType.ALL) private Company publisher;
以下是个使用ManyToOne annotation的例子:
@Entity public class Publisher { @Id private int publisherId; } @Entity public class Magazine { @Id private int magazineId; @ManyToOne private Publisher publisher; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Magazine (magazineId INTEGER NOT NULL, publisher_publisherId INTEGER, PRIMARY KEY (magazineId)) TYPE = innodb; CREATE TABLE Publisher (publisherId INTEGER NOT NULL, PRIMARY KEY (publisherId)) TYPE = innodb; CREATE INDEX I_MGAZINE_PUBLISHER ON Magazine (publisher_publisherId);
如果不希望使用OpenJPA缺省的关联字段名(例如publisher_publisherId),那么可以使用JoinColumn annotation,例如:
@Entity public class Publisher { @Id private int publisherId; } @Entity public class Magazine { @Id private int magazineId; @ManyToOne @JoinColumn(name = "publisherId", referencedColumnName = "publisherId") private Publisher publisher; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Magazine (magazineId INTEGER NOT NULL, publisherId INTEGER, PRIMARY KEY (magazineId)) TYPE = innodb; CREATE TABLE Publisher (publisherId INTEGER NOT NULL, PRIMARY KEY (publisherId)) TYPE = innodb; CREATE INDEX I_MGAZINE_PUBLISHER ON Magazine (publisherId);
3.2.9 One To Many
如果entity A引用多个entity B,而且没有两个entity A引用相同的entity B,那么称A和B之间的关系是One To Many。JPA使用OneToMany annotation来指定这种关系。它有以下属性:
如果使用OneToMany关系,那么缺省情况下JPA将会生成中间表来进行关联,例如:
@Entity public class Publisher { @Id private int publisherId; @OneToMany private List<Magazine> magazines; } @Entity public class Magazine { @Id private int magazineId; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Magazine (magazineId INTEGER NOT NULL, PRIMARY KEY (magazineId)) TYPE = innodb; CREATE TABLE Publisher (publisherId INTEGER NOT NULL, PRIMARY KEY (publisherId)) TYPE = innodb; CREATE TABLE Publisher_Magazine (Publisher_publisherId INTEGER, magazines_magazineId INTEGER) TYPE = innodb; CREATE INDEX I_PBLSGZN_ELEMENT ON Publisher_Magazine (magazines_magazineId); CREATE INDEX I_PBLSGZN_PUBLISHER_PUBLISHERID ON Publisher_Magazine (Publisher_publisherId);
如果不希望使用OpenJPA缺省的关联表名和字段名(例如Publisher_Magazine,Publisher_publisherId),那么可以使用JoinTable annotation,例如:
@Entity public class Publisher { @Id private int publisherId; @OneToMany @JoinTable(name="PublisherMagazine", joinColumns=@JoinColumn(name="publisherId", referencedColumnName="publisherId"), inverseJoinColumns=@JoinColumn(name="magazineId", referencedColumnName="magazineId")) private List<Magazine> magazines; } @Entity public class Magazine { @Id private int magazineId; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Magazine (magazineId INTEGER NOT NULL, PRIMARY KEY (magazineId)) TYPE = innodb; CREATE TABLE Publisher (publisherId INTEGER NOT NULL, PRIMARY KEY (publisherId)) TYPE = innodb; CREATE TABLE PublisherMagazine (publisherId INTEGER, magazineId INTEGER) TYPE = innodb; CREATE INDEX I_PBLSGZN_ELEMENT ON PublisherMagazine (magazineId); CREATE INDEX I_PBLSGZN_PUBLISHERID ON PublisherMagazine (publisherId);
考虑下面这种情况:
@Entity public class Publisher { @Id private int publisherId; @OneToMany() private List<Magazine> magazines; } @Entity public class Magazine { @Id private int magazineId; @ManyToOne() private Publisher publisher; }
在这种情况下,JPA会根据Publisher上的OneToMany关系生成关联表,同时根据Magazine上的ManyToOne关系在Magazine表上生成关联字段。如果希望避免这种冗余,那么可以通过OneToMany上的mappedBy属性来告知JPA:谁拥有这个双向关系。 例如:
@Entity public class Publisher { @Id private int publisherId; @OneToMany(mappedBy="publisher") private List<Magazine> magazines; } @Entity public class Magazine { @Id private int magazineId; @ManyToOne() private Publisher publisher; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Magazine (magazineId INTEGER NOT NULL, publisher_publisherId INTEGER, PRIMARY KEY (magazineId)) TYPE = innodb; CREATE TABLE Publisher (publisherId INTEGER NOT NULL, PRIMARY KEY (publisherId)) TYPE = innodb; CREATE INDEX I_MGAZINE_PUBLISHER ON Magazine (publisher_publisherId);
3.2.10 One To One
如果entity A引用唯一的entity B,而且没有其它entity A引用相同的entity B,那么称A和B之间的关系是One To One。JPA使用OneToOne annotation来指定这种关系。它有以下属性:
以下是几个使用OneToOne关系的例子:
@Entity public class Company { @Id private int id; @OneToOne private Address address; } @Entity public class Address { @Id private int id; @OneToOne private Company company; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下(两个表都生成了跟对方表主键类型相同的字段及其索引,但是并没有显式的外键关联):
CREATE TABLE Address (id INTEGER NOT NULL, company_id INTEGER, PRIMARY KEY (id)) TYPE = innodb; CREATE TABLE Company (id INTEGER NOT NULL, address_id INTEGER, PRIMARY KEY (id)) TYPE = innodb; CREATE INDEX I_ADDRESS_COMPANY ON Address (company_id); CREATE INDEX I_COMPANY_ADDRESS ON Company (address_id);
如果将Address类中company属性的annotation改成@OneToOne(mappedBy = "address"),那么用MappingTool生成的SQL(MySQL数据库)如下(只有Company表生成了跟Address表主键类型相同的字段及其索引,但是并没有显式的外键关联):
CREATE TABLE Address (id INTEGER NOT NULL, PRIMARY KEY (id)) TYPE = innodb; CREATE TABLE Company (id INTEGER NOT NULL, address_id INTEGER, PRIMARY KEY (id)) TYPE = innodb; CREATE INDEX I_COMPANY_ADDRESS ON Company (address_id);
如果不希望使用OpenJPA缺省的关联字段名(例如address_id),那么可以使用JoinColumn annotation,例如:
@Entity public class Company { @Id private int id; @OneToOne @JoinColumn(name = "AddressId", referencedColumnName = "id") private Address address; } @Entity public class Address { @Id private int id; @OneToOne(mappedBy = "address") private Company company; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Address (id INTEGER NOT NULL, PRIMARY KEY (id)) TYPE = innodb; CREATE TABLE Company (id INTEGER NOT NULL, AddressId INTEGER, PRIMARY KEY (id)) TYPE = innodb; CREATE INDEX I_COMPANY_ADDRESS ON Company (AddressId);
3.2.11 Many To Many
如果entity A引用多个entity B,而且其它的entity A也可能引用其中某些entity B,那么称A和B之间的关系是Many To Many。JPA使用ManyToMany annotation来指定这种关系。它有以下属性:
如果使用ManyToMany关系,那么JPA将会生成中间表来进行关联,例如:
@Entity public class Author { @Id private int authorId; @ManyToMany(cascade=CascadeType.ALL) private List<Article> articles; } @Entity public class Article { @Id private int articleId; @ManyToMany(cascade=CascadeType.ALL) private List<Author> authors; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Article (articleId INTEGER NOT NULL, PRIMARY KEY (articleId)) TYPE = innodb; CREATE TABLE Article_Author (Article_articleId INTEGER, authors_authorId INTEGER) TYPE = innodb; CREATE TABLE Author (authorId INTEGER NOT NULL, PRIMARY KEY (authorId)) TYPE = innodb; CREATE TABLE Author_Article (Author_authorId INTEGER, articles_articleId INTEGER) TYPE = innodb; CREATE INDEX I_RTCLTHR_ARTICLE_ARTICLEID ON Article_Author (Article_articleId); CREATE INDEX I_RTCLTHR_ELEMENT ON Article_Author (authors_authorId); CREATE INDEX I_THR_TCL_AUTHOR_AUTHORID ON Author_Article (Author_authorId); CREATE INDEX I_THR_TCL_ELEMENT ON Author_Article (articles_articleId);
如果将Article类中authors属性的annotation改成@ManyToMany(cascade=CascadeType.ALL, mappedBy="articles"),那么用MappingTool生成的SQL(MySQL数据库)如下:
CREATE TABLE Article (articleId INTEGER NOT NULL, PRIMARY KEY (articleId)) TYPE = innodb; CREATE TABLE Author (authorId INTEGER NOT NULL, PRIMARY KEY (authorId)) TYPE = innodb; CREATE TABLE Author_Article (authors_authorId INTEGER, articles_articleId INTEGER) TYPE = innodb; CREATE INDEX I_THR_TCL_AUTHORS_AUTHORID ON Author_Article (authors_authorId); CREATE INDEX I_THR_TCL_ELEMENT ON Author_Article (articles_articleId);
如果不希望使用OpenJPA缺省的关联表名和字段名(例如Author_Article,authors_authorId),那么可以使用JoinTable annotation,例如:
@Entity public class Author { @Id private int authorId; @ManyToMany(cascade=CascadeType.ALL) @JoinTable(name="AuthorArticle", joinColumns=@JoinColumn(name="AuthorId", referencedColumnName="AuthorId"), inverseJoinColumns=@JoinColumn(name="ArticleId", referencedColumnName="ArticleId")) private List<Article> articles; } @Entity public class Article { @Id private int articleId; @ManyToMany(cascade=CascadeType.ALL, mappedBy="articles") private List<Author> authors; }
用MappingTool生成以上两个entity class的SQL(MySQL数据库)如下:
CREATE TABLE Article (articleId INTEGER NOT NULL, PRIMARY KEY (articleId)) TYPE = innodb; CREATE TABLE Author (authorId INTEGER NOT NULL, PRIMARY KEY (authorId)) TYPE = innodb; CREATE TABLE AuthorArticle (AuthorId INTEGER, ArticleId INTEGER) TYPE = innodb; CREATE INDEX I_THRRTCL_AUTHORID ON AuthorArticle (AuthorId); CREATE INDEX I_THRRTCL_ELEMENT ON AuthorArticle (ArticleId);
3.2.12 Order By
由于关系型数据库并不保存records的顺序,因此为了保证集合类型的字段有一致的顺序,必须使用OrderBy annotation。缺省是采用identity values的升序。以下是个简单的例子:
@OrderBy("lastName, firstName") private Collection<Author> authors;
3.2.13 Map Key
JPA在OneToMany或者ManyToMany关系中支持Map类型的字段,关联的entities组成了Map的value。JPA在每个entity中挑选一个字段作为Map的key。MapKey annotation有一个name属性,用来指定被挑选作为key的字段名。如果没有指定name,那么会使用关联entity的identity字段。
3.2.14 Persistent Field Defaults
当某个字段没有使用以上介绍的annotations时,JPA对该字段采取以下的缺省行为:
3.2.15 Column
Column annotation 用来指定列的属性,它有以下可选属性: