Hibernate注解@Entity
通过@Entity注解将一个类声明为一个实体bean(即一个持久化POJO类), @Id注解则声明了该实体bean的标识属性. 其他的映射定义是隐式的.
就是说一个持久化POJO类,除了主键ID需要@Id显示注解,其他列都可以不做任何注解。
写了个main程序测试:代码如下:
- 数据库DDL语句:
1 create table CAT 2 ( 3 id VARCHAR2(32) not null, 4 CAT_NAME VARCHAR2(32), 5 create_time DATE, 6 update_time DATE 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 6 <hibernate-configuration> 7 <session-factory> 8 <!-- 数据库驱动配置 --> 9 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 10 <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> 11 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 12 <property name="connection.username">wxuatuser</property> 13 <property name="connection.password">xlh</property> 14 <property name="show_sql">true</property> 15 16 <!-- hibernate实体类 --> 17 <mapping class="test1.Cat"/> 18 </session-factory> 19 </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 test1; 2 3 import javax.persistence.Entity; 4 import model.BaseEntity; 5 /** 6 * 实体类 7 */ 8 @Entity 9 public class Cat extends BaseEntity{ 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 23 }
main入口
1 package test1; 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 import test1.Cat; 11 12 public class HibernateUtil { 13 14 private static final SessionFactory sessionFactory; 15 16 static { 17 try { 18 Configuration cfg = new Configuration().configure(); 19 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() 20 .applySettings(cfg.getProperties()).buildServiceRegistry(); 21 sessionFactory = cfg.buildSessionFactory(serviceRegistry); 22 } catch (Throwable ex) { 23 // Log exception! 24 throw new ExceptionInInitializerError(ex); 25 } 26 } 27 28 public static Session getSession() throws HibernateException { 29 return sessionFactory.openSession(); 30 } 31 32 public static void main(String[] args) { 33 Session session = HibernateUtil.getSession(); 34 Transaction tx = null; 35 try { 36 tx = session.beginTransaction(); 37 Cat cat = new Cat(); 38 cat.setCat_name("cat5"); 39 session.save(cat); 40 tx.commit(); 41 } catch (RuntimeException e) { 42 if (tx != null) { 43 tx.rollback(); 44 } 45 throw e; 46 } finally { 47 session.close(); 48 } 49 50 } 51 }
源码地址:http://files.cnblogs.com/files/xiluhua/hibernate%40entity.rar
环境:JDK1.6,MAVEN
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。