Hibernate annotations 配置

在原项目中引入hibernate-annotations.jar和ejb3-persistence.jar

 

hibernate.cfg.xml配置改动不大,只是改一下mapping的方式,直接写类名。

 

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6.     <session-factory>
  7.         <!-- Database connection settings -->
  8.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  9.         <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
  10.         <property name="connection.username">root</property>
  11.         <property name="connection.password">1</property>
  12.         <!-- JDBC connection pool (use the built-in) -->
  13.         <property name="connection.pool_size">1</property>
  14.         <!-- SQL dialect -->
  15.         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  16.         <!-- Enable Hibernate's automatic session context management -->
  17.         <property name="current_session_context_class">thread</property>
  18.         <!-- Disable the second-level cache  -->
  19.         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  20.         <!-- Echo all executed SQL to stdout -->
  21.         <property name="show_sql">true</property>
  22.         <!-- Drop and re-create the database schema on startup -->
  23.         <property name="hbm2ddl.auto">update</property>
  24.         <mapping class="org.leo.Order"/>
  25.     </session-factory>
  26. </hibernate-configuration>

要注意的是SessionFactory实例化的方式。

  1. SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

一定要用AnnotationConfiguration

 

实体Bean的原hbm文件不用了。改为加Annotation

 

  1. package org.leo;
  2. import java.io.Serializable;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. @Entity
  9. @Table(name="t_order")
  10. public class Order implements Serializable {
  11.     private static final long serialVersionUID = 8896146925710456113L;
  12.     @Id
  13.     @GeneratedValue(strategy=GenerationType.IDENTITY)
  14.     private int objID;
  15.     private String name;
  16.     public int getObjID() {
  17.         return objID;
  18.     }
  19.     public void setObjID(int objID) {
  20.         this.objID = objID;
  21.     }
  22.     public String getName() {
  23.         return name;
  24.     }
  25.     public void setName(String name) {
  26.         this.name = name;
  27.     }
  28. }

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值