用于持久化POJO类
指定数据库的表,目录和schema
@UniqueConstraints 可以定义表级约束
@Entity
@Table(name = "student",schema = "hibernate")
public class Student {
...
}
注解用于支持乐观锁版本控制。
@Version
@Column(name="OPTLOCK")
public Integer getVersion() { ... }
属性将忽略不被持久化
@Transient
StringgetLengthInMeter() { ... } //transient property
获取想要的时间类型
TemporalType.DATE yyyy-MM-dd
TemporalType.TIME hh:mm:ss
TemporalType.TIMESTAMP yyyy-MM-dd hh:mm:ss 2016-12-07 11:47:58.697这个是会显示到毫秒的
@Basic
@Column(name = "birth")
@Temporal(TemporalType.DATE)
public Date getBirth() {
return birth;
}
注解属性将被持久化为 Blog 或 Clob 类型
1.java.sql.Clob, Character[], char[] 和 java.lang.String 将被持久化为 Clob 类型.
2.java.sql.Blob,Byte[], byte[] 和 serializable type 将被持久化为 Blob 类型。
@Lob
public String getFullText() {
return fullText; // clob type
}
@Lob
public byte[] getFullCode() {
return fullCode; // blog type
}
注解将属性映射到列
name 列名
unique是否在该列上设置唯一约束
nullable 列可空
insertable 该列是否作为生成insert语句的一个列
updatable 该列是否作为生成 update语句的一个列
columnDefinition 默认值
table 定义对应的表(deault 是主表)
length 列长度
precision 精度
scale 长度
覆盖该属性对应的嵌入式对象的列映射
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "phone", column = @Column(name = "phone")),
@AttributeOverride(name = "postCode", column = @Column(name = "postcode"))
})
public Address getAddress() {
return address;
}
需要在对象上添加@Embeddable注解
@Embeddable
public class Address {
private String postCode;
private String phone;
public Address() {
}
public Address(String postCode, String phone) {
this.postCode = postCode;
this.phone = phone;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Address{" +
"postCode='" + postCode + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
相当于
name="address" class="com.hqq.bean.Address">
<property name="phone" column="phone"/>
<property name="postCode" column="postcode"/>
属性为简单类型,则映射为@Basic
•属性对应的类型定义了@Embeddable 注解,则映射为@Embedded
•属性对应的类型实现了Serializable,则属性被映射为@Basic并在一个列中保存该对象的serialized版本。
•属性的类型为java.sql.Clob or java.sql.Blob, 则映射到 @Lob 对应的类型。
将实体Bean中某个属性定义为主键
AUTO - 可以是 identity column, sequence 或者 table 类型,取决于不同底层的数据库
TABLE - 使用table保存id值
IDENTITY - identity column
SEQUENCE - sequence
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
public Integer getId(){ ... }
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {... }
@GenerateValue注解可以定义该标识符的生成策略。
建立实体Bean之间的一对一关系
1.共享同样的主键。 @PrimaryKeyJoinColumn注解定义了一对一的关联关系
@Entity
public class Body {
@Id
public Long getId() { return id; }
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Heart getHeart() {
return heart;
}
...
}
@Entity
public class Heart {
@Id
public Long getId() { ...}
}
2.其中一个实体通过外键关联到另一个实体的主键
通过@JoinColumn注解定义一对一的关联关系
@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="passport_fk")
public Passport getPassport() {
...
}
@Entity
public class Passport implements Serializable {
@OneToOne(mappedBy = "passport")
public Customer getOwner() {
...
}
3.通过关联表来保存两个实体之间的关联关系
@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name= "CustomerPassports",
joinColumns= @JoinColumn(name="customer_fk"),
inverseJoinColumns= @JoinColumn(name="passport_fk")
)
public Passport getPassport() {
...
}
@Entity
public class Passport implements Serializable {
@OneToOne(mappedBy = "passport")
public Customer getOwner() {
...
}
1.简单映射
@Entity()
public class Flightimplements Serializable {
@ManyToOne( cascade = {CascadeType.PERSIST,CascadeType.MERGE} )
@JoinColumn(name="COMP_ID")
public Company getCompany() {
return company;
}
...
}
@JoinColumn 注解是可选的,关键字段默认值和一对一关联的情况相似。
列名为:主题的关联属性名 + 下划线 + 被关联端的主键列名
2.通过关联表的方式来映射 @JoinTable
@Entity()
public class Flightimplements Serializable {
@ManyToOne( cascade = {CascadeType.PERSIST,CascadeType.MERGE} )
@JoinTable(name="Flight_Company",
joinColumns =@JoinColumn(name="FLIGHT_ID"),
inverseJoinColumns =@JoinColumn(name="COMP_ID")
)
public Company getCompany() {
return company;
}
...
}
1.单向
只在一方配置@JoinColumn
@Entity
public class Customer implements Serializable {
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="CUST_ID")
public Set getTickets() {
...
}
@Entity
public class Ticket implements Serializable {
... //no bidir
}
2.双向
@Entity
public class Troop {
@OneToMany(mappedBy="troop")
public Set getSoldiers() {
...
}
@Entity
public class Soldier {
@ManyToOne
@JoinColumn(name="troop_fk")
public Troop getTroop() {
...
}
报错java.lang.StackOverflowError
去掉任何一方的重写的toString方法即可
@Entity
public class Employerimplements Serializable {
@ManyToMany(
targetEntity=org.hibernate.test.metadata.manytomany.Employee.class,
cascade={CascadeType.PERSIST,CascadeType.MERGE}
)
@JoinTable(
name="EMPLOYER_EMPLOYEE",
joinColumns=@JoinColumn(name="EMPER_ID"),
inverseJoinColumns=@JoinColumn(name="EMPEE_ID")
)
public Collection getEmployees() {
return employees;
}
...
}
@Entity
public class Employeeimplements Serializable {
@ManyToMany(
cascade = {CascadeType.PERSIST,CascadeType.MERGE},
mappedBy = "employees",
targetEntity = Employer.class
)
public Collection getEmployers() {
return employers;
}
}
用cascading 实现传播持久化(Transitivepersistence)
CascadeType.PERSIST:
如果一个实体是受管状态,或者当 persist() 函数被调用时,触发级联创建(create)操作
CascadeType.MERGE:
如果一个实体是受管状态,或者当merge() 函数被调用时,触发级联合并(merge)操作。
CascadeType.REMOVE:
当 delete() 函数被调用时,触发级联删除(remove)操作。
CascadeType.REFRESH:
当 refresh() 函数被调用时,出发级联更新(refresh)操作。
CascadeType.ALL:
以上全部