(1)identity主键自增,这种方式依赖于具体的数据库,如果数据库不支持自增主键,那么这个类型是没法用的
public class Users implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "user_id", nullable = false) private int userId;(2)借助一个表来实现主键自增, 通过一个表来实现主键id的自增,这种方式不依赖于具体的数据库,可以解决数据迁移的问题
public class Users implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.TABLE) @Column(name = "user_code", nullable = false) private String userCode;(3)sequence主键自增,通过Sequence来实现表主键自增,这种方式依赖于数据库是否有SEQUENCE,如果没有就不能用
public class Users implements Serializable { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE) @SequenceGenerator(name="seq_user") @Column(name = "user_id", nullable = false) private int userId;此外,一些实现了JPA规范的ORM映射框架,还提供了自己支持的主键生成策略,比如使用Hibernate-Annotation来实现持久化映射,就可以使用Hibernate提供的UUID主键生成策略.
7.一对多双向映射关系 有T_One和T_Many两个表,他们是一对多的关系,注解范例如下主Pojo
@Entity @Table(name = "T_ONE") public class One implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "ONE_ID", nullable = false) private String oneId; @Column(name = "DESCRIPTION") private String description; @OneToMany(cascade = CascadeType.ALL, mappedBy = "oneId")//指向多的那方的pojo的关联外键字段 private Collection<Many> manyCollection; 子Pojo @Entity @Table(name = "T_MANY") public class Many implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "MANY_ID", nullable = false) private String manyId; @Column(name = "DESCRIPTION") private String description; @JoinColumn(name = "ONE_ID", referencedColumnName = "ONE_ID")//设置对应数据表的列名和引用的数据表的列名 @ManyToOne//设置在“一方”pojo的外键字段上 private One oneId; 说明: 一对多双向关联跟多对一是一样的 ,在多端生成一个外键,不生成第三张表来管理对应关系,由外键来管理对应关系 8.多对多映射关系 貌似多对多关系不需要设置级联,以前用hibernate的时候着实为多对多的级联头疼了一阵子,JPA的多对多还需要实际的尝试一下才能有所体会。 估计JPA的多对多也是可以转换成两个一对多的。 第一个Pojo @Entity @Table(name = "T_MANYA") public class ManyA implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "MANYA_ID", nullable = false) private String manyaId; @Column(name = "DESCRIPTION") private String description; @ManyToMany @JoinTable(name = "TMANY1_TMANY2", joinColumns = {@JoinColumn(name = "MANYA_ID", referencedColumnName = "MANYA_ID")} private Collection<ManyB> manybIdCollection; 第二个Pojo @Entity @Table(name = "T_MANYB") public class ManyB implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "MANYB_ID", nullable = false) private String manybId; @Column(name = "DESCRIPTION") private String description; @ManyToMany(mappedBy = "manybIdCollection") private Collection<ManyA> manyaIdCollection;9.一对一外键映射关系
主Pojo @Entity @Table(name = "T_ONEA") public class OneA implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "ONEA_ID", nullable = false) private String oneaId; @Column(name = "DESCRIPTION") private String description; @OneToOne(cascade = CascadeType.ALL, mappedBy = "oneA")//主Pojo这方的设置比较简单,只要设置好级联和映射到从Pojo的外键就可以了。 private OneB oneB; 从Pojo @Entity @Table(name = "T_ONEB") public class OneB implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "ONEA_ID", nullable = false) private String oneaId; @Column(name = "DESCRIPTION") private String description; @JoinColumn(name = "ONEA_ID", unique=ture referencedColumnName = "ONEA_ID", insertable = false)//设置从方指向主方的关联外键,这个ONEA_ID 其实是表T_ONEA的主键 @OneToOne private OneA oneA; 10 大字段 @Lob //对应Blob字段类型 @Column(name = "PHOTO") private Serializable photo; @Lob //对应Clob字段类型 @Column(name = "DESCRIPTION") private String description; 11.瞬时字段 :不需要与数据库映射的字段,在保存的时候不需要保存倒数据库 @Transient private int tempValue; public int getTempValue(){ get tempValue; } public void setTempValue(int value){ this.tempValue = value;}
12.类的继承映射
JPA对于具有父子关系的类,对于父类必须声明继承实体的映射策略,对于继承实体,java.persistence.InheritanceType定义了3种映射策略(跟Hibernate类继承的映射原理相同):
SINGLE_TABLE:父子类都保存在同一个表中,通过字段值进行区分。此方法推荐使用
JOINED:父子类相同的部分保存在同一个表中,不同的部门分开存放,通过连接不同的表获取完整数据。
TABLE_PER_CLASS:每一个类对应自己的表,一般不推荐采用这种方式。
转载于:https://my.oschina.net/zhanghaiyang/blog/551814