`

JPA基本数据类型映射

阅读更多

               /**
                 *  Employ                  
                 * @author Administrator                
                 */
                @Entity
                @Table(name = "T_EMPLOY")


                // @SequenceGenerator(name = "SEQ", sequenceName = "SEQ_SYS_FUNC_MENU",
                // initialValue = 0, allocationSize = 1)   Oracle中序列方式生成主键


                public class Employ {

                //@Id
                // @GeneratedValue(strategy = GenerationType.SEQUENCE, generator ="SEQ")

                //Oracle序列方式生成/主键


                @Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)   //MySQL,SQLSErver自增长方式
                @Column(name = "ID", nullable = false)
                 private Integer id; // 主键,非空,主键生成方式自动


                 @Column(name = "NAME", nullable = false, length = 32)
                 private String name; // 长度32,非空


                 @Temporal(TemporalType.DATE)
                 @Column(name = "BIRTHDAY", nullable = true, length = 32)
                 private Date birthday; // 日期类型,格式:yyyy-MM-dd


                 @Temporal(TemporalType.TIMESTAMP)
                 private Date enterDate; // 日期类型,格式:yyyy-MM-dd HH:mm:ss


                 @Enumerated(EnumType.ORDINAL)
                 @Column(name = "FIRSTSEASON")
                 private Season firstSeason = Season.Spring;// 季节枚举类型,显示索引值


                 @Enumerated(EnumType.STRING)
                 @Column(name = "SECONDSEASON", nullable = false, length = 6)
                 private Season secondSeason = Season.Autumn;// 季节枚举类型,显示字符串


                 @Lob
                 private String bigText; // 文本大字段类型


                 @Lob
                 @Basic(fetch = FetchType.LAZY)
                 private Byte[] fileData; // 二进制字节流,启用延迟加载方式


                 @Transient
                 private String filePath;// 虚拟列,不生成表字段

 

                 public String toString() {
                           return "[ id:" + id + ",name:" + name + ",birthday:" + birthday
    + ",enterDate:" + enterDate + ",firstSeason:" + firstSeason.ordinal()
    + ",secondSeason:" + secondSeason + ",bigText:" + bigText
    + ",filePath:=" + filePath + "]";
 }

                 //getter/setter ... ...       

}

                 //测试方法 

                  /**
                    * @param args
                    */
                  public static void main(String[] args) {
                             // TODO Auto-generated method stub
                            EntityManagerFactory factory = Persistence.createEntityManagerFactory("sample");
                            EntityManager em = factory.createEntityManager();
                            em.getTransaction().begin();
                            Employ employ = new Employ();

                            employ.setName("employ1");
                            employ.setFirstSeason(Season.Spring);
                            employ.setSecondSeason(Season.Autumn);

                            em.persist(employ);
                            em.getTransaction().commit();
                            em.close();
                            factory.close(); 

                  }

                 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics