The first hibernate applet implemented based on the annotations configuration file.
1. Create a common java project first.
2. Import the oracle database driver package and the minimum hibernate package to this project.
The Oracle Database driver package is:
Oracle 10g 10.2.0.4 JDBC_ojdbc14.jar
The minimum Hibernate package is:
Antlr-2.7.6.jar
Commons-collections-3.1.jar
Dom4j-1.6.1.jar
Hibernate3.jar
Javassist-3.4.GA.jar
Jta-1.1.jar
Slf4j-api-1.5.2.jar
Slf4j-nop-1.5.2.jar
Hibernate-annotations.jar
Hibernate-commons-annotations.jar
Ejb3-persistence.jar (this package is hibernate Based on jpa specifications)
3. Create a package named com. neusoft. bean under the src folder and create a file named Person. java under the package. The content is as follows:
Package com. neusoft. bean;
Import javax. persistence. Entity;
Import javax. persistence. Id;
Import javax. persistence. Table;
@ Entity
@ Table (name = "person_anno ")
Public class Person {
Private int id;
Private String name;
@ Id
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
}
4. Create a package named com. neusoft. test under the src package, and then create a Test class named test. java under the package. Content:
Package com. neusoft. test;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. cfg. AnnotationConfiguration;
Import org. hibernate. cfg. Configuration;
Import com. neusoft. bean. Person;
Public class Test {
Public static void main (String [] args ){
Person p = new Person ();
P. setId (1 );
P. setName ("wjl ");
Configuration cfg = new AnnotationConfiguration ();
SessionFactory sf = cfg. configure (). buildSessionFactory ();
Session s = sf. openSession ();
S. beginTransaction (). begin ();
S. save (p );
S. getTransaction (). commit ();
S. close ();
Sf. close ();
}
}
5. Create a hibernate configuration file named hibernate. cfg. xml in the src folder. The content is as follows:
<? Xml version = '1. 0' encoding = 'utf-8'?>
<! DOCTYPE hibernate-configuration PUBLIC
"-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
<Hibernate-configuration>
<Session-factory>
<! -- Database connection settings -->
<Property name = "connection. driver_class"> oracle. jdbc. driver. OracleDriver </property>
<Property name = "connection. url"> jdbc: oracle: thin: @ 192.168.1.102: 1521: oracle </property>
<Property name = "connection. username"> hr </property>
<Property name = "connection. password"> hr </property>
<! -- JDBC connection pool (use the built-in) -->
<Property name = "connection. pool_size"> 1 </property>
<! -- SQL dialect -->
<Property name = "dialect"> org. hibernate. dialect. OracleDialect </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"> create </property>
<Mapping class = "com. neusoft. bean. Person"/>
</Session-factory>
</Hibernate-configuration>
6. Execute Test. java to print the following information in the console:
Hibernate: insert into person_anno (name, id) values (?, ?)
7. view the database. The table PERSON_ANNO is generated and a piece of data is generated.
Author: "extreme scholar"