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