Hibernate中pojo注解介绍

关浩壤
2023-12-01

原文地址:http://blog.sina.com.cn/s/blog_8d8425f301014dls.html

1.设置Pojo为实体

01.@Entity //标识这个pojo是一个jpa实体    

02.public class Users implements Serializable {    

03.} 

2.设置表名

01.@Entity    

02.@Table(name = "users") //指定表名为users    

03.public class Users implements Serializable {    

04.} 

3.设置主键

01.public class Users implements Serializable {    

02.@Id    

03.private String userCode; 

4. 设置字段类型

通过@Column注解设置,包含的设置如下

.name:字段名

.unique:是否唯一

.nullable:是否可以为空

.inserttable:是否可以插入

.updateable:是否可以更新

.columnDefinition: 定义建表时创建此列的DDL

.secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字。

01.@Column(name = "user_code", nullable = false, length=32)//设置属性userCode对应的字段为user_code,长度为32,非空   

02.private String userCode;    

03.@Column(name = "user_wages", nullable = true, precision=12, scale=2)//设置属性wages对应的字段为user_wages,12位数字可保留两位小数,可以为空   

04.private double wages;    

05.@Temporal(TemporalType.DATE)//设置为时间类型    

06.private Date joinDate; 

5.字段排序

在加载数据的时候可以为其指定顺序,使用@OrderBy注解实现

01.@Table(name = "USERS")    

02.public class User {    

03.@OrderBy(name = "group_name ASC, name DESC")    

04.private List books = new ArrayList();    

05.} 

6.主键生成策略

01.public class Users implements Serializable {    

02.@Id    

03.@GeneratedValue(strategy=GenerationType.IDENTITY)//主键自增,注意,这种方式依赖于具体的数据库,如果数据库不支持自增主键,那么这个类型是没法用的   

04.@Column(name = "user_id", nullable = false)    

05.private int userId;    

06. 

07. 

08.public class Users implements Serializable {    

09.@Id    

10.@GeneratedValue(strategy=GenerationType.TABLE)//通过一个表来实现主键id的自增,这种方式不依赖于具体的数据库,可以解决数据迁移的问题   

11.@Column(name = "user_code", nullable = false)    

12.private String userCode;    

13. 

14. 

15.public class Users implements Serializable {    

16.@Id    

17.@GeneratedValue(strategy=GenerationType.SEQUENCE)//通过Sequence来实现表主键自增,这种方式依赖于数据库是否有SEQUENCE,如果没有就不能用   

18.@SequenceGenerator(name="seq_user")    

19.@Column(name = "user_id", nullable = false)    

20.private int userId; 

7.一对多映射关系

有T_One和T_Many两个表,他们是一对多的关系,注解范例如下

主Pojo

01.@Entity    

02.@Table(name = "T_ONE")    

03.public class One implements Serializable {    

04.private static final long serialVersionUID = 1L;    

05.@Id    

06.@Column(name = "ONE_ID", nullable = false)    

07.private String oneId;    

08.@Column(name = "DESCRIPTION")    

09.private String description;    

10.@OneToMany(cascade = CascadeType.ALL, mappedBy = "oneId")//指向多的那方的pojo的关联外键字段   

11.private Collection manyCollection;  

子Pojo

01.@Entity    

02.@Table(name = "T_MANY")    

03.public class Many implements Serializable {    

04.private static final long serialVersionUID = 1L;    

05.@Id    

06.@Column(name = "MANY_ID", nullable = false)    

07.private String manyId;    

08.@Column(name = "DESCRIPTION")    

09.private String description;    

10. 

11.@JoinColumn(name = "ONE_ID", referencedColumnName = "ONE_ID")//设置对应数据表的列名和引用的数据表的列名   

12.@ManyToOne//设置在“一方”pojo的外键字段上    

13.private One oneId;  

8.多对多映射关系

貌似多对多关系不需要设置级联,以前用hibernate的时候着实为多对多的级联头疼了一阵子,JPA的多对多还需要实际的尝试一下才能有所体会。

估计JPA的多对多也是可以转换成两个一对多的。

第一个Pojo

01.@Entity    

02.@Table(name = "T_MANYA")    

03.public class ManyA implements Serializable {    

04.private static final long serialVersionUID = 1L;    

05.@Id    

06.@Column(name = "MANYA_ID", nullable = false)    

07.private String manyaId;    

08.@Column(name = "DESCRIPTION")    

09.private String description;    

10.@ManyToMany    

11.@JoinTable(name = "TMANY1_TMANY2", joinColumns = {@JoinColumn(name = "MANYA_ID", referencedColumnName = "MANYA_ID")}, inverseJoinColumns = {@JoinColumn(name = "MANYB_ID", referencedColumnName = "MANYB_ID")})   

12.private Collection manybIdCollection;  

第二个Pojo

01.@Entity    

02.@Table(name = "T_MANYB")    

03.public class ManyB implements Serializable {    

04.private static final long serialVersionUID = 1L;    

05.@Id    

06.@Column(name = "MANYB_ID", nullable = false)    

07.private String manybId;    

08.@Column(name = "DESCRIPTION")    

09.private String description;    

10.@ManyToMany(mappedBy = "manybIdCollection")    

11.private Collection manyaIdCollection;  

9.一对一映射关系

主Pojo

01.@Entity    

02.@Table(name = "T_ONEA")    

03.public class OneA implements Serializable {    

04.private static final long serialVersionUID = 1L;    

05.@Id    

06.@Column(name = "ONEA_ID", nullable = false)    

07.private String oneaId;    

08.@Column(name = "DESCRIPTION")    

09.private String description;    

10.@OneToOne(cascade = CascadeType.ALL, mappedBy = "oneA")//主Pojo这方的设置比较简单,只要设置好级联和映射到从Pojo的外键就可以了。   

11.private OneB oneB;   

从Pojo

01.@Entity    

02.@Table(name = "T_ONEB")    

03.public class OneB implements Serializable {    

04.private static final long serialVersionUID = 1L;    

05.@Id    

06.@Column(name = "ONEA_ID", nullable = false)    

07.private String oneaId;    

08.@Column(name = "DESCRIPTION")    

09.private String description;    

10.@JoinColumn(name = "ONEA_ID", referencedColumnName = "ONEA_ID", insertable = false, updatable = false)//设置从方指向主方的关联外键,这个ONEA_ID其实是表T_ONEA的主键   

11.@OneToOne    

12.private OneA oneA;  

10 大字段

01.@Lob //对应Blob字段类型    

02.@Column(name = "PHOTO")    

03.private Serializable photo;    

04.@Lob //对应Clob字段类型    

05.@Column(name = "DESCRIPTION")    

06.private String description; 

11.瞬时字段

不需要与数据库映射的字段,在保存的时候不需要保存倒数据库

01.@Transient    

02.private int tempValue;    

03. 

04.public int getTempValue(){    

05.get tempValue;    

06.}    

07. 

08.public void setTempValue(int value){    

09.this.tempValue = value;    

10.}

 类似资料: