zoukankan      html  css  js  c++  java
  • Hibernate,JPA注解@Entity

    通过@Entity注解将一个类声明为一个实体bean(即一个持久化POJO类), @Id注解则声明了该实体bean的标识属性. 其他的映射定义是隐式的.

    就是说一个持久化POJO类,除了主键ID需要@Id显示注解,其他列都可以不做任何注解。

    用例代码如下:

    • 数据库DDL语句:
    1 create table CAT
    2 (
    3   id          VARCHAR2(32 CHAR) not null,
    4   create_time TIMESTAMP(6),
    5   update_time TIMESTAMP(6),
    6   cat_name    VARCHAR2(255 CHAR)
    7 )
    • hibernate.cfg.xml
     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <!DOCTYPE hibernate-configuration
     3  PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
     4  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     5 <hibernate-configuration>
     6     <session-factory>
     7         <!-- 数据库驱动配置 -->
     8         <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
     9         <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
    10         <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
    11         <property name="connection.username">wxuatuser</property>
    12         <property name="connection.password">xlh</property>
    13         <property name="show_sql">true</property>
    14         <!-- 自动执行DDL属性是update,不是true -->
    15         <property name="hbm2ddl.auto">update</property>
    16         <!-- hibernate实体类 -->
    17         
    18         <mapping class="a1_Entity.Cat"/>
    19         
    20     </session-factory>
    21 </hibernate-configuration>
    • java类
    实体类 - 基类
     1 package model;
     2 import java.io.Serializable;
     3 import java.util.Date;
     4 import javax.persistence.Column;
     5 import javax.persistence.GeneratedValue;
     6 import javax.persistence.Id;
     7 import javax.persistence.MappedSuperclass;
     8 import org.hibernate.annotations.GenericGenerator;
     9 
    10 /**
    11  * 实体类 - 基类
    12  */
    13 @MappedSuperclass
    14 public class BaseEntity implements Serializable {
    15 
    16     private static final long serialVersionUID = -6718838800112233445L;
    17 
    18     private String id;// ID
    19     private Date create_time;// 创建日期
    20     private Date update_time;// 修改日期
    21     @Id
    22     @Column(length = 32, nullable = true)
    23     @GeneratedValue(generator = "uuid")
    24     @GenericGenerator(name = "uuid", strategy = "uuid")
    25     public String getId() {
    26         return id;
    27     }
    28     public void setId(String id) {
    29         this.id = id;
    30     }
    31     @Column(updatable = false)
    32     public Date getCreate_time() {
    33         return create_time;
    34     }
    35     public void setCreate_time(Date create_time) {
    36         this.create_time = create_time;
    37     }
    38     public Date getUpdate_time() {
    39         return update_time;
    40     }
    41     public void setUpdate_time(Date update_time) {
    42         this.update_time = update_time;
    43     }
    44     @Override
    45     public int hashCode() {
    46         return id == null ? System.identityHashCode(this) : id.hashCode();
    47     }
    48     @Override
    49     public boolean equals(Object obj) {
    50         if (this == obj) {
    51             return true;
    52         }
    53         if (obj == null) {
    54             return false;
    55         }
    56         if (getClass().getPackage() != obj.getClass().getPackage()) {
    57             return false;
    58         }
    59         final BaseEntity other = (BaseEntity) obj;
    60         if (id == null) {
    61             if (other.getId() != null) {
    62                 return false;
    63             }
    64         } else if (!id.equals(other.getId())) {
    65             return false;
    66         }
    67         return true;
    68     }
    69 }
    实体类
     1 package a1_Entity;
     2 import javax.persistence.Entity;
     3 
     4 import model.BaseEntity;
     5 
     6 @Entity
     7 public class Cat extends BaseEntity{
     8     /**
     9      * 实体类
    10      */
    11     private static final long serialVersionUID = -2776330321385582872L;
    12     
    13     private String cat_name;
    14 
    15     public String getCat_name() {
    16         return cat_name;
    17     }
    18 
    19     public void setCat_name(String cat_name) {
    20         this.cat_name = cat_name;
    21     }
    22 }
    Dao
     1 package daoUtil;
     2 
     3 import org.hibernate.HibernateException;
     4 import org.hibernate.Session;
     5 import org.hibernate.SessionFactory;
     6 import org.hibernate.Transaction;
     7 import org.hibernate.cfg.Configuration;
     8 import org.hibernate.service.ServiceRegistry;
     9 import org.hibernate.service.ServiceRegistryBuilder;
    10 
    11 public class HibernateUtil {
    12 
    13     private static final SessionFactory sessionFactory;
    14 
    15     static {
    16         try {
    17             Configuration cfg = new Configuration().configure();
    18             ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
    19                     .applySettings(cfg.getProperties()).buildServiceRegistry();
    20             sessionFactory = cfg.buildSessionFactory(serviceRegistry);
    21         } catch (Throwable ex) {
    22             // Log exception!
    23             throw new ExceptionInInitializerError(ex);
    24         }
    25     }
    26 
    27     public static Session getSession() throws HibernateException {
    28         return sessionFactory.openSession();
    29     }
    30 
    31     public static Object save(Object obj){
    32         Session session = HibernateUtil.getSession();
    33         Transaction tx = null;
    34         try {
    35             tx = session.beginTransaction();
    36             session.save(obj);
    37             tx.commit();
    38         } catch (RuntimeException e) {
    39             if (tx != null) {
    40                 tx.rollback();
    41             }
    42             throw e;
    43         } finally {
    44             session.close();
    45         }
    46         return obj;
    47     }
    48     
    49     public static void delete(Class<?> clazz,String id){
    50         Session session = HibernateUtil.getSession();
    51         Transaction tx = null;
    52         try {
    53             tx = session.beginTransaction();
    54             Object obj = session.get(clazz,id);
    55             session.delete(obj);
    56             tx.commit();
    57         } catch (RuntimeException e) {
    58             if (tx != null) {
    59                 tx.rollback();
    60             }
    61             throw e;
    62         } finally {
    63             session.close();
    64         }
    65     }
    66 }
    main
     1 package a1_Entity;
     2 
     3 import daoUtil.HibernateUtil;
     4 
     5 public class Test_Entity {
     6 
     7     public static void main(String[] args) {
     8         Cat cat = new Cat();
     9         cat.setId("1");
    10         cat.setCat_name("test1@Entity");
    11         HibernateUtil.save(cat);
    12     }
    13 }

    环境:JDK1.6,MAVEN

    源码地址:http://files.cnblogs.com/files/xiluhua/hibernate%40Entity.rar

  • 相关阅读:
    必会重构技巧(二):使用多态替换条件
    必会重构技巧(五):划分职责
    Flickr 网站架构分析
    必会重构技巧(三):提取接口
    WCF 一步一步 发布 WCF服务 到 IIS (图)
    LINQ 图解
    在Silverlight中如何创建WCF Service
    必会重构技巧(四):提取工厂类
    技术汇总:第十四章:电脑端生成支付宝二维码支付
    HDU 4463 Outlets 2012年亚洲区域赛杭州赛区现场赛K题
  • 原文地址:https://www.cnblogs.com/xiluhua/p/4359711.html
Copyright © 2011-2022 走看看