On the Internet, we can see many examples of getting started with hibernate, but many of them let hibernate manage the connection pool by itself. Here is an example of using the data source directly released by weblogic. The procedure is as follows:
1. Write a class for persistence.
- PackageCom. jarie. Business. Organization;
-
- ImportJava. Io.Serializable;
-
- /**
- * <P> title: </P>
- * <P> Description: Permission </P>
- * <P> copyright: Copyright (c) 2003 </P>
- * <P> company: www.jagie.com </P>
- * @ Author jagie
- * @ Version 1.0
- */
-
- Public ClassPermissionImplements Serializable{
- Private StringID;// PK
- Private StringName;// Name
- Private StringDescription;// Description
- Private StringModule;// Module ID
- Private StringPower;// Weight, for example, browse $ add $ Delete $ change
- Private IntScope;// Range, 0: myself, 1: This unit, 2: All units
- Public Static VoidMain (String[] ARGs ){
- }
- Public StringGETID (){
- ReturnID;
- }
- Public VoidSetid (StringID ){
- This. ID = ID;
- }
- Public StringGetname (){
- ReturnName;
- }
- Public VoidSetname (StringName ){
- This. Name = Name;
- }
- Public StringGetdescription (){
- ReturnDescription;
- }
- Public VoidSetdescription (StringDescription ){
- This. Description = description;
- }
- Public StringGetmodule (){
- ReturnModule;
- }
- Public VoidSetmodule (StringModule ){
- This. Module = module;
- }
- Public StringGetpower (){
- ReturnPower;
- }
- Public VoidSetpower (StringPower ){
- This. Power = power;
- }
- Public IntGetscope (){
- ReturnScope;
- }
- Public VoidSetscope (IntScope ){
- This. Scope = scope;
- }
- }
2. Compile an XML file named permission. HBM. xml. Make sure that the XML file is with permission. Class at runtime.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<class name="com.jagie.business.organization.Permission" table="SYS_Permission">
<id name="ID">
<generator class="uuid.hex"/>
</id>
<property name="name"/>
<property name="module"/>
<property name="description"/>
<property name="power"/>
<property name="scope"/>
</class>
3. Configure the connection pool and Data Source on weblogic. The JNDI name of my data source is oilds4. modify the hibernate. properties file under classpath and save
A. Add a line: hibernate. dialect net. SF. hibernate. dialect. oracledialect
B. Locate the JNDI datasource section and set hibernate. Connection. datasource oilds below
C. Find plugin connectionprovider and remove hibernate. Connection. provider_class.
Net. SF. hibernate. Connection. cececonnectionprovider comments
D. Find the transaction API and remove hibernate. transaction. manager_lookup_class.
Net. SF. hibernate. transaction. weblogictransactionmanagerlookup
E. Save the changes.
5. Compile a JNDI. properties file in the class path. This file is very important to consider flexibility and prevent hard encoding. The content is as follows:
Java. Naming. Factory. Initial = weblogic. JNDI. wlinitialcontextfactory
Java. Naming. provider. url = T3: // localhost: 7001 (My WebLogic Server is on the local machine, maybe you need to modify it as needed)
6. Okay, everything is ready. Let's write a test class to test the power of hibernate. The source code is as follows.
- PackageCom. jarie. Business. Organization;
-
- ImportNet. SF. hibernate. Session;
- ImportNet. SF. hibernate. transaction;
- ImportNet. SF. hibernate. sessionfactory;
- ImportNet. SF. hibernate. cfg. configuration;
- ImportNet. SF. hibernate. tool. hbm2ddl. schemaexport;
-
- ImportJavax. naming.Initialcontext;
- ImportJavax. naming.Context;
- ImportJavax. SQL .*;
- ImportJava. SQL .*;
- ImportJava. util .*;
- ImportCom. jarie. utils. J2EE .*;
-
- Public ClassTest {
- Private StaticSessionfactory sessions;
-
- Public Static VoidMain (String[] ARGs)Throws Exception{
- Configuration conf =NewConfiguration (). addclass (permission.Class);
- Sessions = Conf. buildsessionfactory ();
- // Generate and output SQL statements to files (current directory) and databases
- Schemaexport dbexport =NewSchemaexport (CONF );
- Dbexport. setoutputfile ("SQL .txt ");
- Dbexport. Create (True,True);
-
-
- // Start ......
- Session S = sessions. opensession ();
- Transaction T = S. begintransaction ();
-
- // 1. Create an object and fill in data in common usage
- Permission p1 =NewPermission ();
- P1.setname ("1111 ");
-
- // 2. Persistence
- S. Save (P1 );
- // At this time, P1 can be found in the database
- T. Commit ();
- S. Close ();
-
- }
- }