`
netxdiy
  • 浏览: 680426 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

hibernate3.6 annotation

阅读更多

hibernate3.6中,要使用annotations,请引入‍

hibernate-distribution-3.6.0.Final\lib\jpa‍\hibernate-jpa-2.0-api-1.0.0.Final.jar这个jar文件。

 

引:“‍部分合并到core了,另一部分合并到entity manager了,

其实是希望用户尽量使用JPA标准的annotation了,annotation的定义可以去看JPA规范。”


1.目前最新的hibernate版本为hibernate3.6 ,此版本已经包含了Hibernate Annotations 和 Hibernate EntityManager,所以现在只要下载hibernate3.6就ok了。

官网地址为:http://www.hibernate.org/

或:http://nchc.dl.sourceforge.net/project/hibernate/hibernate3/3.6.0.Final/hibernate-distribution-3.6.0.Final-dist.zip

即必须导入的包都在改文件夹下:\lib\required(解压后的)

注:在该解压包中:hibernate-distribution-3.6.0.Final\documentation\下有2个文件夹,javadoc和manual,一个是API,一个是帮助文档

2.使用log4j(虽然hibernate使用的是slf4j)

下载地址:http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.16/apache-log4j-1.2.16.zip

即需要的jar包:log4j-1.2.16.jar

3.单元测试Junit4.9

下载地址:https://github.com/downloads/KentBeck/junit/junit4.9b2.zip

即需要的jar包:junit-4.9b2.jar

4. ejb3-persistence.jar的下载地址:http://www.java2s.com/Code/JarDownload/ejb3-persistence.jar.zip

  如果没有改jar包;将有可能出现:javax/persistence/EntitvListeners提示

5. slf4j的下载地址:    http://www.slf4j.org/dist/slf4j-1.6.1.zip

即需要slf4j-log4j12-1.6.1.jar包,此包是slf4j转log4j的jar,当你使用log4j的时候所需要的,如果想要使用其他的日志,这需要该包下其他的转换jar包(因开发而异)。

6. hibernate-jpa-2.0-api-1.0.0.Final.jar;该jar在hibernate3.6解压文件中:hibernate-distribution-3.6.0.Final\lib\jpa

如果没有该jar包;将有可能出现:javax.persistence.Caheable的提示。

7. jar全部准备好后,开始建立项目,名称Hibernate_FirstProject,并在src文件夹下导入相应的配置文件: hibernate.cfg.xml和log4j.properties该文件都可以在hibernate3.6包中找到:

hibernate-distribution-3.6.0.Final\project\etc;

但是hibernate.cfg.xml的内容有点少了,所以最好可以去帮助文档里copy一份过来,修改一下就好啦,内容如下:

hibernate.cfg.xml

view plaincopy to clipboardprint?
01.<?xml version='1.0' encoding='utf-8'?>   
02.<!DOCTYPE hibernate-configuration PUBLIC   
03.        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
04.        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">   
05.  
06.<hibernate-configuration>   
07.  
08.    <session-factory>   
09.  
10.        <!-- Database connection settings -->   
11.        <property name="connection.driver_class">   
12.              com.microsoft.sqlserver.jdbc.SQLServerDriver   
13.        </property>   
14.        <property name="connection.url">   
15.             jdbc:sqlserver://localhost:1433;DatabaseName=db_FirstProject   
16.        </property>   
17.        <property name="connection.username">sa</property>   
18.        <property name="connection.password"></property>   
19.  
20.        <!-- JDBC connection pool (use the built-in) -->   
21.        <!--<property name="connection.pool_size">1</property>-->   
22.  
23.        <!-- SQL dialect -->   
24.        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>   
25.  
26.        <!-- Enable Hibernate's automatic session context management -->   
27.        <property name="current_session_context_class">thread</property>   
28.  
29.        <!-- Disable the second-level cache  -->   
30.        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider   
31.        </property>   
32.  
33.        <!-- Echo all executed SQL to stdout -->   
34.        <property name="show_sql">true</property>   
35.  
36.        <!-- Drop and re-create the database schema on startup -->   
37.        <property name="hbm2ddl.auto">update</property>   
38.  
39.     </session-factory>   
40.  
41.</hibernate-configuration>  
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">
              com.microsoft.sqlserver.jdbc.SQLServerDriver
        </property>
        <property name="connection.url">
             jdbc:sqlserver://localhost:1433;DatabaseName=db_FirstProject
        </property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <!--<property name="connection.pool_size">1</property>-->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider
        </property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

     </session-factory>

</hibernate-configuration>

        <property name="connection.driver_class">驱动</property>
        <property name="connection.url">URL地址</property>
        <property name="connection.username">你自己的用户名</property>
        <property name="connection.password">密码</property>

这些就相当于编写JDBC,而hibernate就把这些写入配置文件中

还需要修改的为:

<property name="dialect">使用相应的sql的方言,方言的值可以在帮助文档中找到,即表示hibernate会翻译成相应数据的sql语句</property>

view plaincopy to clipboardprint?
01.DB2             org.hibernate.dialect.DB2Dialect    
02.DB2 AS/400      org.hibernate.dialect.DB2400Dialect    
03.DB2 OS390       org.hibernate.dialect.DB2390Dialect    
04.PostgreSQL      org.hibernate.dialect.PostgreSQLDialect    
05.MySQL           org.hibernate.dialect.MySQLDialect    
06.MySQL with InnoDB   org.hibernate.dialect.MySQLInnoDBDialect    
07.MySQL with MyISAM   org.hibernate.dialect.MySQLMyISAMDialect    
08.Oracle(any version)     org.hibernate.dialect.OracleDialect    
09.Oracle 9i       org.hibernate.dialect.Oracle9iDialect    
10.Oracle 10g      org.hibernate.dialect.Oracle10gDialect    
11.Sybase          org.hibernate.dialect.SybaseDialect    
12.Sybase Anywhere     org.hibernate.dialect.SybaseAnywhereDialect    
13.Microsoft SQL Server    org.hibernate.dialect.SQLServerDialect    
14.SAP DB          org.hibernate.dialect.SAPDBDialect    
15.Informix        org.hibernate.dialect.InformixDialect    
16.HypersonicSQL       org.hibernate.dialect.HSQLDialect    
17.Ingres          org.hibernate.dialect.IngresDialect    
18.Progress        org.hibernate.dialect.ProgressDialect    
19.Mckoi SQL       org.hibernate.dialect.MckoiDialect    
20.Interbase       org.hibernate.dialect.InterbaseDialect    
21.Pointbase       org.hibernate.dialect.PointbaseDialect    
22.FrontBase       org.hibernate.dialect.FrontbaseDialect    
23.Firebird        org.hibernate.dialect.FirebirdDialect   
DB2    org.hibernate.dialect.DB2Dialect 
DB2 AS/400   org.hibernate.dialect.DB2400Dialect 
DB2 OS390   org.hibernate.dialect.DB2390Dialect 
PostgreSQL   org.hibernate.dialect.PostgreSQLDialect 
MySQL    org.hibernate.dialect.MySQLDialect 
MySQL with InnoDB  org.hibernate.dialect.MySQLInnoDBDialect 
MySQL with MyISAM  org.hibernate.dialect.MySQLMyISAMDialect 
Oracle(any version)  org.hibernate.dialect.OracleDialect 
Oracle 9i   org.hibernate.dialect.Oracle9iDialect 
Oracle 10g   org.hibernate.dialect.Oracle10gDialect 
Sybase    org.hibernate.dialect.SybaseDialect 
Sybase Anywhere  org.hibernate.dialect.SybaseAnywhereDialect 
Microsoft SQL Server  org.hibernate.dialect.SQLServerDialect 
SAP DB    org.hibernate.dialect.SAPDBDialect 
Informix   org.hibernate.dialect.InformixDialect 
HypersonicSQL   org.hibernate.dialect.HSQLDialect 
Ingres    org.hibernate.dialect.IngresDialect 
Progress   org.hibernate.dialect.ProgressDialect 
Mckoi SQL   org.hibernate.dialect.MckoiDialect 
Interbase  org.hibernate.dialect.InterbaseDialect 
Pointbase   org.hibernate.dialect.PointbaseDialect 
FrontBase   org.hibernate.dialect.FrontbaseDialect 
Firebird   org.hibernate.dialect.FirebirdDialect 

8. 编写自己的实体类Teacher.java

    
view plaincopy to clipboardprint?
01.package com.hero.model;   
02.  
03.import java.util.Date;   
04.  
05.import javax.persistence.Column;   
06.import javax.persistence.Entity;   
07.import javax.persistence.GeneratedValue;   
08.import javax.persistence.GenerationType;   
09.import javax.persistence.Id;   
10.import javax.persistence.Table;   
11.import javax.persistence.Temporal;   
12.import javax.persistence.TemporalType;   
13.import javax.persistence.Transient;   
14.  
15.import org.hibernate.annotations.GenericGenerator;   
16.  
17.@Entity   
18.public class Teacher {   
19.    private int id;   
20.    private String password;   
21.    private String username;   
22.    public Teacher(){}   
23.       
24.    @Id   
25.    @GeneratedValue(strategy=GenerationType.AUTO)   
26.    public int getId() {   
27.        return id;   
28.    }   
29.    public void setId(int id) {   
30.        this.id = id;   
31.    }   
32.    public String getPassword() {   
33.        return password;   
34.    }   
35.    public void setPassword(String password) {   
36.        this.password = password;   
37.    }   
38.    public String getUsername() {   
39.        return username;   
40.    }   
41.    public void setUsername(String username) {   
42.        this.username = username;   
43.    }   
44.       
45.}  
package com.hero.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

@Entity
public class Teacher {
 private int id;
 private String password;
 private String username;
 public Teacher(){}
 
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 
}
 

9.编写自己的测试类TeacherTest.java

   
view plaincopy to clipboardprint?
01.package com.hero.model;   
02.  
03.import static org.junit.Assert.*;   
04.  
05.import java.util.Date;   
06.  
07.import junit.framework.TestCase;   
08.  
09.import org.hibernate.Session;   
10.import org.hibernate.SessionFactory;   
11.import org.hibernate.cfg.Configuration;   
12.  
13.public class TeacherTest extends TestCase{   
14.    private SessionFactory sessionFactory;   
15.  
16.    @Override   
17.    protected void setUp() throws Exception {   
18.        // A SessionFactory is set up once for an application   
19.        sessionFactory = new Configuration()   
20.                .configure() // configures settings from hibernate.cfg.xml   
21.                .buildSessionFactory();   
22.    }   
23.  
24.    @Override   
25.    protected void tearDown() throws Exception {   
26.        if ( sessionFactory != null ) {   
27.            sessionFactory.close();   
28.        }   
29.    }   
30.  
31.    @SuppressWarnings({ "unchecked" })   
32.    public void testBasicUsage() {   
33.        // create a couple of events...   
34.        Teacher t=new Teacher();   
35.        t.setPassword("tea1");   
36.        t.setUsername("teacher2");   
37.        Session session = sessionFactory.openSession();   
38.        session.beginTransaction();   
39.        session.save(t);   
40.                session.getTransaction().commit();   
41.        session.close();   
42.    }   
43.  
44.}  
package com.hero.model;

import static org.junit.Assert.*;

import java.util.Date;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TeacherTest extends TestCase{
 private SessionFactory sessionFactory;

 @Override
 protected void setUp() throws Exception {
  // A SessionFactory is set up once for an application
        sessionFactory = new Configuration()
                .configure() // configures settings from hibernate.cfg.xml
                .buildSessionFactory();
 }

 @Override
 protected void tearDown() throws Exception {
  if ( sessionFactory != null ) {
   sessionFactory.close();
  }
 }

 @SuppressWarnings({ "unchecked" })
 public void testBasicUsage() {
  // create a couple of events...
  Teacher t=new Teacher();
  t.setPassword("tea1");
  t.setUsername("teacher2");
  Session session = sessionFactory.openSession();
  session.beginTransaction();
  session.save(t);
                session.getTransaction().commit();
  session.close();
 }

}
 

在hibernate3.6中,已经不再使用AnnotationConfiguration了,该内容的东西都被加入到了Configuration中

10.在hibernate.cfg.xml中加入如下代码:

     <mapping class="com.hero.model.Teacher" />

11.测试,perfect,ok!!!

 

分享到:
评论

相关推荐

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt; org.whvcse.model.Userinfo ...

    hibernate annotation 中文文档

    2.4.3.6. 生成的属性 2.4.4. 继承 2.4.5. 关于单个关联关系的注解 2.4.5.1. 延迟选项和获取模式 2.4.6. 关于集合类型的注解 2.4.6.1. 参数注解 2.4.6.2. 更多的集合类型 2.4.7. 缓存 2.4.8. 过滤器 2.4.9. 查询 3. ...

    hibernate annotation帮助文档

    2.4.3.6. 生成的属性 2.4.4. 继承 2.4.5. 关于单个关联关系的注解 2.4.5.1. 延迟选项和获取模式 2.4.6. 关于集合类型的注解 2.4.6.1. 参数注解 2.4.6.2. 更多的集合类型 2.4.7. 缓存 2.4.8. 过滤器 2.4.9. ...

    Hibernate+中文文档

    3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...

    hibernate3.2中文文档(chm格式)

    3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...

    HibernateAPI中文版.chm

    3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...

    Hibernate 中文 html 帮助文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...

    Hibernate中文详细学习文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    最全Hibernate 参考文档

    3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. JTA和Session的自动绑定 3.8.4. JMX部署 4. 持久化类(Persistent ...

    hibernate 体系结构与配置 参考文档(html)

    3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 ...

    Hibernate3+中文参考文档

    3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. JTA和Session的自动绑定 3.8.4. JMX部署 4. 持久化类(Persistent ...

    Hibernate参考文档

    3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 ...

    低清版 大型门户网站是这样炼成的.pdf

    3.6 struts 2其他标签详解 166 3.6.1 actionerror与actionmessage标签详解 166 3.6.2 fielderror标签详解 167 3.6.3 tree与treenode标签详解 168 3.7 struts 2全力支持ajax 174 3.7.1 struts 2中原始ajax技术...

    hibernate_reference中文文档.pdf

    3.6. 实现 NamingStrategy .................................................. 44 3.7. XML 配置文件 ......................................................... 45 3.8. J2EE 应用程序服务器的集成 ..............

    Spring.3.x企业应用开发实战(完整版).part2

    7.5.1 @annotation() 7.5.2 execution() 7.5.3 args()和@args() 7.5.4 within() 7.5.5 @within()和@target() 7.5.6 target()的this() 7.6 @AspectJ进阶 7.6.1 切点复合运算 7.6.2 命名切点 7.6.3 ...

    Spring3.x企业应用开发实战(完整版) part1

    7.5.1 @annotation() 7.5.2 execution() 7.5.3 args()和@args() 7.5.4 within() 7.5.5 @within()和@target() 7.5.6 target()的this() 7.6 @AspectJ进阶 7.6.1 切点复合运算 7.6.2 命名切点 7.6.3 ...

    Spring中文帮助文档

    3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. ...

    Spring API

    3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. ...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    8.4.2 Annotation与部署描述文件 315 8.5 开发有状态的Session Bean 318 8.6 Session Bean的生命周期 321 8.6.1 无状态Session Bean的生命周期 321 8.6.2 有状态Session Bean的生命周期 322 8.6.3 定制Session Bean...

Global site tag (gtag.js) - Google Analytics