文章摘抄 Hibernate 官网(https://docs.jboss.org/hibernate/jpa/2.2/api/javax/persistence/Embeddable.html),目录及部分源码参考w3c 官网(https://www.w3cschool.cn/java/jpa-entitymanager.html)。主要是在自己的整理过程中,发现w3c梳理的东西过于简洁,实在让我无法准确理解,于是我就拿着w3c的目录来整理文档,但是力求于相对的准确,也就懒得力求于百度,直接ctrl cv 了。
特别声明:本人英文翻译较菜,怕译议后不恰,况且现在的google翻译是真的牛逼,但是还是写了一部分,仅仅为了自己日后方便阅读。
JPA 全称为 JAVA Persistence API ,JPA 吸取了目前Java 持久化技术的有点,旨在规范、简化Java对象的持久化技术工作。使用JPA持久化对象,并不是依赖于某一个ORM框架
Specifies the mapped column for a persistent property or field. If no Column annotation is specified, the default values apply.
//Example 1:
@Column(name="DESC", nullable=false, length=512)
public String getDescription() { return description; }
// Example 2:
// columnDefinition 定义字段的类型 ,且不能为空(也可以设置默认值,如:columnDefinition = "BIT default 0")
//table 包含列的表的名称
@Column(name="DESC",
columnDefinition="CLOB NOT NULL",
table="EMP_DETAIL")
@Lob
public String getDescription() { return description; }
// Example 3:
// precision 精度(字段长度), scale 范围(小数位数)
@Column(name="ORDER_COST", updatable=false, insertable=false, precision=12, scale=2)
public BigDecimal getCost() { return cost; }
Specifies that the property or field is not persistent. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.
//官方上介绍了很多生成策略,这里附上mysql的基本用法
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
//IDENTITY:主键由数据库自动生成(主要是自动增长型) (mysql 这种方式也常用,但是主要是数据库底层去自增长,移植性较差)
//AUTO:主键由程序控制。
@IdClass(com.acme.EmployeePK.class)
@Entity
public class Employee {
@Id String empName;
@Id Date birthDay;
...
}
//Example 1:
@Lob @Basic(fetch=LAZY)
@Column(name="REPORT")
protected String report;
// Example 2:
@Lob @Basic(fetch=LAZY)
@Column(name="EMP_PIC", columnDefinition="BLOB NOT NULL")
protected byte[] pic;
//Example 1:
@Basic
protected String name;
//Example 2:
@Basic(fetch=LAZY)
protected String getName() { return name; }
Specifies a single-valued association to another entity that has one-to-one multiplicity. It is not normally necessary to specify the associated target entity explicitly since it can usually be inferred from the type of the object being referenced. If the relationship is bidirectional, the non-owning side must use the mappedBy element of the OneToOne annotation to specify the relationship field or property of the owning side.
The OneToOne annotation may be used within an embeddable class to specify a relationship from the embeddable class to an entity class. If the relationship is bidirectional and the entity containing the embeddable class is on the owning side of the relationship, the non-owning side must use the mappedBy element of the OneToOne annotation to specify the relationship field or property of the embeddable class. The dot (".") notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.
//Example 1: One-to-one association that maps a foreign key column
// On Customer class:
// 单项关联(也就是第一段英文的前半句)
@OneToOne(optional=false)
@JoinColumn(
name="CUSTREC_ID", unique=true, nullable=false, updatable=false)
public CustomerRecord getCustomerRecord() { return customerRecord; }
// On CustomerRecord class:
// 双向关联(这里解释一下non-owning side ,我翻译成非控制一方:CustomerRecord 中是存在 Customer 的ID 的,故CustomerRecord 是可以改变CustomerID,所以CustomerRecord 是控制一方,而 Customer 是非控制一方 或叫做被控制一方。)
// 那么 在控制一方 里关联的被控制一方上 需要有 mappedBy ,当然mappedBy 关联的可以使对象类型也可以是基本数据类型
@OneToOne(optional=false, mappedBy="customerRecord")
public Customer getCustomer() { return customer; }
//Example 2: One-to-one association that assumes both the source and target share the same primary key values.
// On Employee class:
@Entity
public class Employee {
@Id Integer id;
@OneToOne @MapsId
EmployeeInfo info;
...
}
// On EmployeeInfo class:
@Entity
public class EmployeeInfo {
@Id Integer id;
...
}
Example 3: One-to-one association from an embeddable class to another entity.
@Entity
public class Employee {
@Id int id;
@Embedded LocationDetails location;
...
}
@Embeddable
public class LocationDetails {
int officeNumber;
@OneToOne ParkingSpot parkingSpot;
...
}
@Entity
public class ParkingSpot {
@Id int id;
String garage;
@OneToOne(mappedBy="location.parkingSpot") Employee assignedTo;
...
}
//Example 1:
@Embeddable public class EmploymentPeriod {
@Temporal(DATE) java.util.Date startDate;
@Temporal(DATE) java.util.Date endDate;
...
}
//Example 2:
@Embeddable public class PhoneNumber {
protected String areaCode;
protected String localNumber;
@ManyToOne PhoneServiceProvider provider;
...
}
@Entity public class PhoneServiceProvider {
@Id protected String name;
...
}
// Example 3:
@Embeddable public class Address {
protected String street;
protected String city;
protected String state;
@Embedded protected Zipcode zipcode;
}
@Embeddable public class Zipcode {
protected String zip;
protected String plusFour;
}
The AttributeOverride, AttributeOverrides, AssociationOverride, and AssociationOverrides annotations may be used to override mappings declared or defaulted by the embeddable class.
//Example:
@Embedded
@AttributeOverrides({
@AttributeOverride(name="startDate", column=@Column("EMP_START")),
@AttributeOverride(name="endDate", column=@Column("EMP_END"))
})
public EmploymentPeriod getEmploymentPeriod() { ... }
//Example 1:
@EmbeddedId
protected EmployeePK empPK;
//Example 2:
@Embeddable
public class DependentId {
String name;
EmployeeId empPK; // corresponds to primary key type of Employee
}
@Entity
public class Dependent {
// default column name for "name" attribute is overridden
@AttributeOverride(name="name", @Column(name="dep_name"))
@EmbeddedId DependentId id;
...
@MapsId("empPK")
@ManyToOne Employee emp;
}
Designates a ManyToOne or OneToOne relationship attribute that provides the mapping for an EmbeddedId primary key, an attribute within an EmbeddedId primary key, or a simple primary key of the parent entity.
The value element specifies the attribute within a composite key to which the relationship attribute corresponds. If the entity’s primary key is of the same Java type as the primary key of the entity referenced by the relationship, the value attribute is not specified
//Example:
// parent entity has simple primary key
@Entity
public class Employee {
@Id long empId;
String name;
...
}
// dependent entity uses EmbeddedId for composite key
@Embeddable
public class DependentId {
String name;
long empid; // corresponds to primary key type of Employee
}
@Entity
public class Dependent {
@EmbeddedId DependentId id;
...
@MapsId("empid") // maps the empid attribute of embedded id
@ManyToOne Employee emp;
}
If the collection is defined using generics(泛型集合) to specify the element type, the associated target entity type need not be specified; otherwise the target entity class must be specified. If the relationship is bidirectional, the mappedBy element must be used to specify the relationship field or property of the entity that is the owner of the relationship.
The OneToMany annotation may be used within an embeddable class contained within an entity class to specify a relationship to a collection of entities. If the relationship is bidirectional, the mappedBy element must be used to specify the relationship field or property of the entity that is the owner of the relationship. When the collection is a java.util.Map, the cascade element and the orphanRemoval element apply to the map value.
// Example 1: One-to-Many association using generics
// In Customer class:
// 顾客相对于订单,一对多的关系
@OneToMany(cascade=ALL, mappedBy="customer")
public Set<Order> getOrders() { return orders; }
// In Order class:
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }
// Example 2: One-to-Many association without using generics
// In Customer class:
@OneToMany(targetEntity=com.acme.Order.class, cascade=ALL,
mappedBy="customer")
public Set getOrders() { return orders; }
// In Order class:
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() { return customer; }
// Example 3: Unidirectional One-to-Many association using a foreign key mapping (暂未用到过该用法)
// In Customer class:
@OneToMany(orphanRemoval=true)
@JoinColumn(name="CUST_ID") // join column is in table for Order
public Set<Order> getOrders() {return orders;}
Specifies a many-valued association with many-to-many multiplicity.
Every many-to-many association has two sides, the owning side and the non-owning, or inverse, side. The join table is specified on the owning side. If the association is bidirectional, either side may be designated as the owning side. If the relationship is bidirectional, the non-owning side must use the mappedBy element of the ManyToMany annotation to specify the relationship field or property of the owning side.
the association,任何一方都可以被指定为拥有方
the relationship,非拥有方要使用mappedBy
The ManyToMany annotation may be used within an embeddable class contained within an entity class to specify a relationship to a collection of entities. If the relationship is bidirectional and the entity containing the embeddable class is the owner of the relationship, the non-owning side must use the mappedBy element of the ManyToMany annotation to specify the relationship field or property of the embeddable class. The dot (".") notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded attribute. The value of each identifier used with the dot notation is the name of the respective embedded field or property.
// Example 1:
// In Customer class:
@ManyToMany
@JoinTable(name="CUST_PHONES")
public Set<PhoneNumber> getPhones() { return phones; }
// In PhoneNumber class:
@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }
Example 2:
// In Customer class:
@ManyToMany(targetEntity=com.acme.PhoneNumber.class)
public Set getPhones() { return phones; }
// In PhoneNumber class:
@ManyToMany(targetEntity=com.acme.Customer.class, mappedBy="phones")
public Set getCustomers() { return customers; }
Example 3:
// In Customer class:
@ManyToMany
@JoinTable(name="CUST_PHONE",
joinColumns=
@JoinColumn(name="CUST_ID", referencedColumnName="ID"),
inverseJoinColumns=
@JoinColumn(name="PHONE_ID", referencedColumnName="ID")
)
public Set<PhoneNumber> getPhones() { return phones; }
// In PhoneNumberClass:
@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }
//Example:
@JoinTable(
name="CUST_PHONE",
joinColumns=
@JoinColumn(name="CUST_ID", referencedColumnName="ID"),
inverseJoinColumns=
@JoinColumn(name="PHONE_ID", referencedColumnName="ID")
)
//Example:
@ManyToOne
@JoinColumn(name="ADDR_ID")
public Address getAddress() { return address; }
Example: unidirectional one-to-many association using a foreign key mapping
// In Customer class
@OneToMany
@JoinColumn(name="CUST_ID") // join column is in table for Order
public Set<Order> getOrders() {return orders;}