hibernate3.6 annotation

    技术2024-08-13  63

    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

    <?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>

    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

        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

       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!!!

     

     

     

     

    最新回复(0)