核心提示:JPA 注解的几个要点 1.设置Pojo为实体 @Entity //标识这个pojo是一个jpa实体 public class Users implements Serializable{ } 2.设置表名 @Entity @Table (name= users ) //指定表名为users public class Users implements Serializable{ } 3.设置主键 public JPA 注解的几个要点 1.设置Pojo为实体 @Entity //标识这个pojo是一个jpa实体 public class Users implements Serializable { } 2.设置表名 @Entity @Table(name = "users") //指定表名为users public class Users implements Serializable { } 3.设置主键 public class Users implements Serializable { @Id private String userCode; 4. 设置字段类型 通过@Column注解设置,包含的设置如下 .name:字段名 .unique:是否唯一 .nullable:是否可以为空 .inserttable:是否可以插入 .updateable:是否可以更新 .columnDefinition: 定义建表时创建此列的DDL .secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字。 @Column(name = "user_code", nullable = false, length=32)//设置属性userCode对应的字段为user_code,长度为32,非空 private String userCode; @Column(name = "user_wages", nullable = true, precision=12, scale=2)//设置属性wages对应的字段为user_wages,12位数字可保留两位小数,可以为空 private double wages; @Temporal(TemporalType.DATE)//设置为时间类型 private Date joinDate; 5.字段排序 在加载数据的时候可以为其指定顺序,使用@OrderBy注解实现 @Table(name = "USERS") public class User { @OrderBy(name = "group_name ASC, name DESC") private List books = new ArrayList(); } 6.主键生成策略 public class Users implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY)//主键自增,注意,这种方式依赖于具体的数据库,如果数据库不支持自增主键,那么这个类型是没法用的 @Column(name = "user_id", nullable = false) private int userId; public class Users implements Serializable { @Id @GeneratedValue(strategy=GenerationType.TABLE)//通过一个表来实现主键id的自增,这种方式不依赖于具体的数据库,可以解决数据迁移的问题 @Column(name = "user_code", nullable = false) private String userCode; public class Users implements Serializable { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)//通过Sequence来实现表主键自增,这种方式依赖于数据库是否有SEQUENCE,如果没有就不能用 @SequenceGenerator(name="seq_user") @Column(name = "user_id", nullable = false) private int userId; 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")}, inverseJoinColumns = {@JoinColumn(name = "MANYB_ID", referencedColumnName = "MANYB_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", referencedColumnName = "ONEA_ID", insertable = false, updatable = 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; }
@Entity --声明为一个实体bean
@Table (name= "promotion_info" ) --为实体bean映射指定表(表名="promotion_info) @Id --声明了该实体bean的标识属性 @GeneratedValue --可以定义标识字段的生成策略. @Transient --将忽略这些字段和属性,不用持久化到数据库 @Column (name= "promotion_remark" )--声明列(字段名= "promotion_total" ) 属性还包括(length= 200 等) @Temporal (TemporalType.TIMESTAMP)--声明时间格式 @Enumerated --声明枚举 @Version --声明添加对乐观锁定的支持 @OneToOne --可以建立实体bean之间的一对一的关联 @OneToMany --可以建立实体bean之间的一对多的关联 @ManyToOne --可以建立实体bean之间的多对一的关联 @ManyToMany --可以建立实体bean之间的多对多的关联 @Formula --一个SQL表达式,这种属性是只读的,不在数据库生成属性(可以使用sum、average、max等) @Entity @Table (name= "promotion_info" ) public class Promotion implements Serializable { //AUTO--可以是identity类型的字段,或者sequence类型或者table类型,取决于不同的底层数据库 @Id @GeneratedValue (strategy = GenerationType.AUTO) private Long id; @Column (name= "group_start_amount" ) private Integer groupStartAmount= 0 ; @Column (name= "promotion_remark" ,length= 200 ) //@Lob 如果是文章内容可以使用 只需要把length=200去掉就可以了 private String remark; //DATE - java.sql.Date //TIME - java.sql.Time //TIMESTAMP - java.sql.Timestamp @Temporal (TemporalType.TIMESTAMP) @Column (name= "start_time" ) private Date startTime; //显示0 隐藏1 public static enum DisplayType { 显示,隐藏 } @Enumerated (value = EnumType.ORDINAL) //ORDINAL序数 private DisplayType displayType = DisplayType.显示; @Version private Integer version; //CascadeType.PERSIST -- 触发级联创建(create) //CascadeType.MERGE -- 触发级联合并(update) //FetchType.LAZY -- 延迟加载 @ManyToOne (cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch = FetchType.LAZY) private PromotionGroup promotionGroup; //单向ManyToMany //@JoinTable(关联的表名) //joinColumns -- promotion关联的列的外键 //inverseJoinColumns -- largess 关联列的外键 @ManyToMany (cascade = {CascadeType.PERSIST,CascadeType.MERGE}) @JoinTable (name= "promotion_largess" ,joinColumns={ @JoinColumn (name= "promotion_id" )},inverseJoinColumns={ @JoinColumn (name= "largess_id" )}) private Set<Largess> largess; //get set 省略.... } @Entity @Table (name= "promotion_group" ) public class PromotionGroup implements Serializable { @Id @GeneratedValue (strategy = GenerationType.AUTO) private Long id; //mappedBy的值"promotionGroup"指向owner(Promotion)端的关联属性,并且是双向关系 @OneToMany (mappedBy= "promotionGroup" ,cascade=CascadeType.ALL) private List<Promotion> promotion; //get set 省略.... } @Entity @Table (name= "largess" ) public class Largess implements Serializable { @Id @GeneratedValue (strategy = GenerationType.AUTO) private Long id; //1.sql语句中的字段和表名都应该和数据库相应,而不是类中的字段, //若带有参数如la.id= id,这个=id才是类中属性 //2.操作字段一定要用别名 @Formula (select max(la.id) from largess as la) private int maxId; @Formula (select COUNT(la.id) from largess la) private int count; @Transient private String img //get set 省略.... }
http://guyinglong.iteye.com/blog/520461