【09-26】hibernate学习笔记

时间:2023-03-09 00:57:53
【09-26】hibernate学习笔记

主键生成策略

@Id
//根据底层数据库决定,mysql-->auto_increment
@GeneratedValue(strategy=GenerationType.AUTO) //使用数据表
@TableGenerator(name="table_name",allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE,generator="table_name") //使用序列
@SequenceGenerator(name="sequence_name",allocationSize=25)
@GeneratedValue(strategy=GenerationType.SEQUENCE) //使用数据库的自增长字段
@GeneratedValue(strategy=GenerationType.IDENTITY)、 //使用uuid2,16byte
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@GeneratedValue(generator = "uuid2")
@Column(columnDefinition = "BINARY(16)")
@Id
private UUID id;

日期配置

//只有指定为java.util.Date 时@Temporal才是必须的,因为hibernate无法区分使用的具体Date子类
@Temporal(TemporalType.TIMESTAMP)
@Column(name="create_time")
private java.util.Date createTime;

其它注解

//不参与orm的属性
@Transient
private String instance; //乐观锁的version配置
@Version
private int version;

实体关系映射

package org.hibernate.annotations;

/**
* Cascade types (can override default EJB3 cascades
*/
public enum CascadeType {
ALL,
PERSIST,
MERGE,
REMOVE,
REFRESH,
DELETE,
SAVE_UPDATE,
REPLICATE,
DELETE_ORPHAN,
LOCK,
EVICT
} public enum FetchType {
LAZY, EAGER
}

单边一对多

@Entity
@Table(name="man")
class Man{
@Id
private Integer id; @OnToMany(fetch=FetchType.EAGER)
@JoinColumns(value={@JoinColumn(name="man_id",referencedColumnName="id")})
private List<Book> books;
}