本文共 10862 字,大约阅读时间需要 36 分钟。
jpa 表注释和字段注释
JPA annotations are used in mapping java objects to the database tables, columns etc. Hibernate is the most popular implement of JPA specification and provides some additional annotations. Today we will look into JPA annotations as well as Hibernate annotations with brief code snippets.
JPA批注用于将Java对象映射到数据库表,列等。Hibernate是JPA规范中最流行的实现,并提供了一些其他批注。 今天,我们将研究带有简短代码段的JPA注释以及Hibernate注释。
is a form of metadata that can be added to Java source code. Java annotations can be read from source files. It can also be embedded in and read from class files generated by the compiler. This allows annotations to be retained by JVM at run-time.
是一种可以添加到Java源代码中的元数据。 可以从源文件中读取Java注释。 它也可以嵌入到编译器生成的类文件中并从中读取。 这允许注释在运行时由JVM保留。
JPA annotations are not part of standard JDK, so you will get it when you add any implementation framework. For example, below hibernate maven dependency will get you JPA annotations too.
JPA批注不是标准JDK的一部分,因此在添加任何实现框架时都将获得它。 例如,在HibernateMaven依赖项下面也会为您提供JPA批注。
org.hibernate.javax.persistence hibernate-jpa-2.1-api 1.0.0.Final
Let us look at some of the important JPA annotations. Note that these annotations are present in javax.persistence
package.
让我们看一些重要的JPA批注。 请注意,这些注释存在于javax.persistence
包中。
javax.persistence.Entity
: Specifies that the class is an entity. This annotation can be applied on Class, Interface of Enums.import javax.persistence.Entity;@Entitypublic class Employee implements Serializable {}
javax.persistence.Entity
:指定该类是一个实体。 该注释可以应用于“类”,“枚举接口”。 @Table
: It specifies the table in the database with which this entity is mapped. In the example below the data will be stores in the “employee” table. Name attribute of @Table annotation is used to specify the table name.import javax.persistence.Entity;import javax.persistence.Table;@Entity@Table(name = "employee")public class Employee implements Serializable {}
@Table
:它指定数据库中与该实体映射的表。 在下面的示例中,数据将存储在“员工”表中。 @Table批注的Name属性用于指定表名。 @Column
: Specify the column mapping using @Column annotation. Name attribute of this annotation is used for specifying the table’s column name.import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.Table;@Entity@Table(name = "employee")public class Employee implements Serializable { @Column(name = "employee_name") private String employeeName;}
@Column
:使用@Column批注指定列映射。 此批注的Name属性用于指定表的列名。 @Id
: This annotation specifies the primary key of the entity.import javax.persistence.*;@Entity@Table(name = "employee")public class Employee implements Serializable { @Id @Column(name = "id") private int id;}
@Id
:此注释指定实体的主键。 @GeneratedValue
: This annotation specifies the generation strategies for the values of primary keys.import javax.persistence.*;@Entity@Table(name = "employee")public class Employee implements Serializable { @Id @Column(name = "id") @GeneratedValue(strategy=SEQUENCE, generator="ID_SEQ") private int id;}
@GeneratedValue
:此批注指定主键值的生成策略。 @Version
: We can control versioning or concurrency using this annotation.import javax.persistence.*;@Entity@Table(name = "employee")public class Employee implements Serializable { @Version @Column(name = "version") private Date version;}
@Version
:我们可以使用此注释来控制版本控制或并发。 @OrderBy
: Sort your data using @OrderBy annotation. In example below, it will sort all employees_address by their id in ascending order.@OrderBy("id asc")private Set employee_address;
@OrderBy
:使用@OrderBy批注对数据进行排序。 在下面的示例中,它将按其ID升序对所有employee_address进行排序。 @Transient
: Every non static and non-transient property of an entity is considered persistent, unless you annotate it as @Transient.@TransientPrivate int employeePhone;
@Transient
:实体的每个非静态和非瞬态属性都被视为持久属性,除非您将其注释为@Transient。 @Lob
: Large objects are declared with @Lob.@Lobpublic String getEmployeeAddress() { return employeeAddress;}
@Lob
:大对象用@Lob声明。 The above set of annotation are most commonly used JPA annotations to define an entity.
上面的批注是最常用的JPA批注,用于定义实体。
We have another set of annotations that are used to specify the association mapping between different tables and entities.
我们还有另一组注释,用于指定不同表和实体之间的关联映射。
We will take an example considering the below mentioned scenario.
我们将考虑下面提到的情况作为示例。
@OneToOne
@OneToOne
@Entity@Table(name = "employee")public class Employee implements Serializable { @Id @Column(name = "id") @GeneratedValue private int id; @OneToOne(cascade = CascadeType.MERGE) @PrimaryKeyJoinColumn private EmployeeDetail employeeDetail;} @Entity@Table(name = "employeeDetail")public class EmployeeDetail implements Serializable { @Id @Column(name = "id") private int id;}
Points to note:
注意事项:
Communication and CommunicationDetail are linked through a foreign key, so @OneToOne and @JoinColumn annotations can be used. In snippet mentioned below, the id genereated for Communication will be mapped to ‘communication_id’ column of CommunicationDetail table. @MapsId is used for the same.
Communication和CommunicationDetail通过外键链接,因此可以使用@OneToOne和@JoinColumn批注。 在下面提到的代码段中,为Communication生成的id将映射到CommunicationDetail表的'communication_id'列。 @MapsId用于相同。
@Entity@Table(name = "communicationDetail")public class CommunicationDetail implements Serializable { @Id @Column(name = "id") @GeneratedValue private int id; @OneToOne @MapsId @JoinColumn(name = "communicationId") private Communication communication;} @Entity@Table(name = "communication")public class Communication implements Serializable { @Id @Column(name = "ID") @GeneratedValue private Integer id; @OneToOne(mappedBy = "communication", cascade = CascadeType.ALL) private CommunicationDetail communicationDetail;}
@ManyToOne
@ManyToOne
@Entity@Table(name = "employee")public class Employee implements Serializable { @ManyToOne @JoinColumn(name = "statusId") private EmployeeStatus status;}
@OneToMany
@OneToMany
@Entity@Table(name = "employee")public class Employee implements Serializable { @OneToMany(mappedBy = "employee", fetch = FetchType.EAGER) @OrderBy("firstName asc") private Set communications;}
@PrimaryKeyJoinColumn
@PrimaryKeyJoinColumn
@Entity@Table(name = "employee")public class Employee implements Serializable { @Id @Column(name = "id") @GeneratedValue private int id; @OneToOne(cascade = CascadeType.MERGE) @PrimaryKeyJoinColumn private EmployeeDetail employeeDetail;}
@JoinColumn
@JoinColumn
@ManyToOne@JoinColumn(name = "statusId")private EmployeeStatus status;
@JoinTable
: @JoinTable and mappedBy should be used for entities linked through an association table.
@JoinTable
:@JoinTable和mappedBy应该用于通过关联表链接的实体。
@MapsId
: Two entities with shared key can be persisted using @MapsId annotation.
@MapsId
:可以使用@MapsId注释保留具有共享密钥的两个实体。
@OneToOne@MapsId@JoinColumn(name = "communicationId")private Communication communication;
Now let us try to understand the inheritance mapping annotation in Hibernate.
现在让我们尝试了解Hibernate中的继承映射注释。
Hibernate supports the three basic inheritance mapping strategies:
Hibernate支持三种基本的继承映射策略:
we will consider example for each type.
我们将考虑每种类型的示例。
@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="cartype", discriminatorType=DiscriminatorType.STRING ) @DiscriminatorValue("Car")public class Car { } @Entity@DiscriminatorValue("BMW")public class BMW extends Car { }每个类层次结构的表–每个类层次结构策略的单个表。
@Entity@Inheritance(strategy=InheritanceType.JOINED)public class Ship implements Serializable {} @Entity@PrimaryKeyJoinColumnpublic class Titanic extends Ship {}每个类/子类的表–联接子类策略。
@Entity@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)public class Aeroplane implements Serializable {}每个具体类的表。
@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="cartype", discriminatorType=DiscriminatorType.STRING )@DiscriminatorColumn:顾名思义,此列为描述符,此注释为SINGLE_TABLE和JOINED继承映射策略指定了discriminator列。
That’s all for JPA and Hibernate annotations.
JPA和Hibernate注释就这些了。
Reference: ,
参考: ,
翻译自:
jpa 表注释和字段注释
转载地址:http://pumzd.baihongyu.com/