标签:
@MappedSuperclass的用法
用在实体的继承过程中的父类上;
父类Cat
1 package com.xj.model; 2 3 import javax.persistence.GeneratedValue; 4 import javax.persistence.Id; 5 import javax.persistence.MappedSuperclass; 6 7 @MappedSuperclass 8 public class Cat { 9 private Long id; 10 private String name; 11 @Id 12 @GeneratedValue 13 public Long getId() { 14 return id; 15 } 16 public void setId(Long id) { 17 this.id = id; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 }
子类1 BeijingCat
1 package com.xj.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.Table; 5 6 @Entity 7 @Table(name="t_beijingcat") 8 public class BeijingCat extends Cat { 9 private String beijing; 10 11 public String getBeijing() { 12 return beijing; 13 } 14 15 public void setBeijing(String beijing) { 16 this.beijing = beijing; 17 } 18 19 }
子类2 ShanghaiCat
1 package com.xj.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.Table; 5 6 @Entity 7 @Table(name="t_shanghaiCat") 8 public class ShanghaiCat extends Cat { 9 private String shanghai; 10 11 public String getShanghai() { 12 return shanghai; 13 } 14 15 public void setShanghai(String shanghai) { 16 this.shanghai = shanghai; 17 } 18 }
结果生成 两张表t_beijingcat、 t_shanghaicat
如果不用@MappedSuperclass 而用@Entity替换的话,只会生成一张cat表,字段有:id,name,shanghaicat,bejingcat
主键生成规则
目前@注解支持4种生成策略:
GenerationType.AUTO:根据数据库自己决定
GenerationType.TABLE:使用指定表决定主键的取值
GenerationType.SEQUENCE:使用sequence决定主键取值,适合支持sequence的数据库。
GenerationType.IDENTITY:identity类型的主键
父类
1 @MappedSuperclass 2 public abstract class AutoIDEntity implements Serializable { 3 4 private static final long serialVersionUID = 1L; 5 6 protected Long id; 7 8 protected Long version; 9 10 @Id 11 @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_STORE") 12 public Long getId() { 13 return id; 14 } 15 public void setId(Long id) { 16 this.id = id; 17 } 18 @Version 19 public Long getVersion() { 20 return version; 21 } 22 public void setVersion(Long version) { 23 this.version = version; 24 } 25 }
子类
1 @Entity 2 @org.hibernate.annotations.Entity(dynamicUpdate=true) 3 @SequenceGenerator(name="SEQ_STORE",sequenceName="SEQ_PAYMENTTEMPLATE_ID",allocationSize=1) 4 @Table(name = "TBL_NMC_PAYMENTTEMPLATE") 5 public class PaymentTemplate extends AutoIDEntity { 6 7 private static final long serialVersionUID = 1L; 8 9 /** 10 * 表达式(销卡通道、通道占比(针对发卡销卡)、是否最后处理通道) 11 */ 12 private String express; 13 /** 14 * 模板名称 15 */ 16 private String name; 17 /** 18 * 卡种 19 */ 20 private CardType cardType; 21 /** 22 * 描述信息 23 */ 24 private String describe; 25 /** 26 * 开关 27 */ 28 private Status status; 29 /** 30 * 创建者 31 */ 32 private String operator; 33 /** 34 * 更新时间 35 */ 36 private Date updateDate; 37 38 @Column(columnDefinition = "VARCHAR(2500)") 39 public String getExpress() { 40 return express; 41 } 42 43 public void setExpress(String express) { 44 this.express = express; 45 } 46 47 @Column(columnDefinition = "VARCHAR(50)") 48 public String getName() { 49 return name; 50 } 51 52 public void setName(String name) { 53 this.name = name; 54 } 55 56 @Enumerated(value = EnumType.STRING) 57 @Column(columnDefinition = "VARCHAR(20)") 58 public CardType getCardType() { 59 return cardType; 60 } 61 62 public void setCardType(CardType cardType) { 63 this.cardType = cardType; 64 } 65 66 @Column(columnDefinition = "VARCHAR(100)") 67 public String getDescribe() { 68 return describe; 69 } 70 71 public void setDescribe(String describe) { 72 this.describe = describe; 73 } 74 75 @Enumerated(value = EnumType.STRING) 76 @Column(columnDefinition = "VARCHAR(20)") 77 public Status getStatus() { 78 return status; 79 } 80 81 public void setStatus(Status status) { 82 this.status = status; 83 } 84 85 @Column(columnDefinition = "VARCHAR(100)") 86 public String getOperator() { 87 return operator; 88 } 89 90 public void setOperator(String operator) { 91 this.operator = operator; 92 } 93 94 public Date getUpdateDate() { 95 return updateDate; 96 } 97 98 public void setUpdateDate(Date updateDate) { 99 this.updateDate = updateDate; 100 } 101 102 }
这是其中一个实体类,我使用的Oracle的数据库,主键采用oracle的序列,但是问题来了,我发现我的id增长不是以
1
递增,而是以
50
递增的,当时我就很纳闷了,后面我就上网查,原来也有人遇到了这个问题,原来是少了allocationSize=
1
这个,原来JPA默认的递增大小是
50
。同时initialValue默认为
0
,一般都是设置为initialValue=
1
修改后的如下所示:
@SequenceGenerator(name="SEQ_STORE",sequenceName="SEQ_PAYMENTTEMPLATE_ID",allocationSize=1)
还有一个要注意问题:注解到底是写在 get方法上,还是写在属性上,这个一定要统一,千万不能混淆使用,我推荐写在getter方法上。
Hibernate 使用Enumerated_EnumType映射枚举字段
Enumerated 和 EnumType 是 jpa 的两个注解。这两个注解用来映射枚举字段
枚举类
1 package com.lyx; 2 3 /** 4 * Created by liyanxin on 2014/12/24. 5 */ 6 public enum Gender { 7 8 MAIL("男性"), FMAIL("女性"); 9 10 private String value; 11 12 private Gender(String value) { 13 this.value = value; 14 } 15 16 @Override 17 public String toString() { 18 return this.value; 19 } 20 }
实体类
1 package com.lyx; 2 3 import java.io.Serializable; 4 5 import javax.persistence.*; 6 7 @Entity 8 @Table(name = "tb_user") 9 public class User implements Serializable { 10 11 12 @Id 13 @GeneratedValue(strategy = GenerationType.IDENTITY) 14 private Integer id; 15 private String name; 16 private int age; 17 18 @ManyToOne 19 @JoinColumn(name = "role_id", unique = false, nullable = false) 20 private Role role; 21 22 @Enumerated(EnumType.STRING) 23 @Column(name = "user_gender") 24 private Gender gender; 25 26 ....................... 27 }
标签:
原文地址:http://www.cnblogs.com/shangxiaofei/p/4745033.html