现在的位置: 首页 > 综合 > 正文

利用weblogic的数据源作为hibernate的数据源的例子

2013年08月13日 ⁄ 综合 ⁄ 共 3669字 ⁄ 字号 评论关闭
在网上,我们可以看到许多关于hibernate入门的例子,但是好多是让hibernate自己管理连接池的。我在这里给出一个直接利用weblogic 发布的数据源的例子。步骤如下

1.写一个准备用于持久化的类
  1. package com.jagie.business.organization;
  2.  
  3. import java.io.Serializable;
  4.  
  5. /**
  6.  * <p>Title: </p>
  7.  * <p>Description: 权限</p>
  8.  * <p>Copyright: Copyright (c) 2003</p>
  9.  * <p>Company: www.jagie.com</p>
  10.  * @author Jagie
  11.  * @version 1.0
  12.  */
  13.  
  14. public class Permission implements Serializable {
  15.   private String ID;//pk
  16.   private String name;//名称
  17.   private String description;//描述
  18.   private String module;//模块id
  19.   private String power;//权值,$分隔的操作id例如:browse$add$delete$change
  20.   private int scope;//范围,0:本人,1:本单位,2:所有单位
  21.   public static void main(String[] args) {
  22.   }
  23.   public String getID() {
  24.     return ID;
  25.   }
  26.   public void setID(String ID) {
  27.     this.ID = ID;
  28.   }
  29.   public String getName() {
  30.     return name;
  31.   }
  32.   public void setName(String name) {
  33.     this.name = name;
  34.   }
  35.   public String getDescription() {
  36.     return description;
  37.   }
  38.   public void setDescription(String description) {
  39.     this.description = description;
  40.   }
  41.   public String getModule() {
  42.     return module;
  43.   }
  44.   public void setModule(String module) {
  45.     this.module = module;
  46.   }
  47.   public String getPower() {
  48.     return power;
  49.   }
  50.   public void setPower(String power) {
  51.     this.power = power;
  52.   }
  53.   public int getScope() {
  54.     return scope;
  55.   }
  56.   public void setScope(int scope) {
  57.     this.scope = scope;
  58.   }
  59. }
2.编写一个xml文件,名称为Permission.hbm.xml,一定要确保在运行时该xml文件和Permission.class在一起
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <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>
</hibernate-mapping>
3.在weblogic 上配置连接池和数据源,我的数据源的jndi名字为OilDS
4.修改classpath下的hibernate.properties文件,并保存

a.添加一行:hibernate.dialect net.sf.hibernate.dialect.OracleDialect
b.找到JNDI Datasource这一段,在下面设置hibernate.connection.datasource OilDS
c.找到Plugin ConnectionProvider部分,去掉hibernate.connection.provider_class 
net.sf.hibernate.connection.DatasourceConnectionProvider一句的注释
d.找到 Transaction API部分,去掉hibernate.transaction.manager_lookup_class 
net.sf.hibernate.transaction.WeblogicTransactionManagerLookup一句的注释
e.保存修改

5.在类路径中编写一个jndi.properties文件,为了考虑灵活性,防止硬编码,该文件非常重要,内容如下

java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3://localhost:7001(我的weblogic服务器就在本机上,也许你的需要适当修改)

6.好啦,万事俱备,让我们写一个Test类来测试一下hibernate的威力好了,原码如下.

  1. package com.jagie.business.organization;
  2.  
  3. import net.sf.hibernate.Session;
  4. import net.sf.hibernate.Transaction;
  5. import net.sf.hibernate.SessionFactory;
  6. import net.sf.hibernate.cfg.Configuration;
  7. import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
  8.  
  9. import javax.naming.InitialContext;
  10. import javax.naming.Context;
  11. import javax.sql.*;
  12. import java.sql.*;
  13. import java.util.*;
  14. import com.jagie.utils.j2ee.*;
  15.  
  16. public class Test {
  17.   private static SessionFactory sessions;
  18.  
  19.   public static void main(String[] args) throws Exception {
  20.     
  21.     Configuration conf = new Configuration().addClass(Permission.class);
  22.     sessions = conf.buildSessionFactory();
  23.    
  24.     //生成并输出sql到文件(当前目录)和数据库
  25.     SchemaExport dbExport = new SchemaExport(conf);
  26.     dbExport.setOutputFile("sql.txt");
  27.     dbExport.create(truetrue);
  28.  
  29.  
  30.     //start......
  31.     Session s = sessions.openSession();
  32.     Transaction t = s.beginTransaction();
  33.  
  34.     //1.用普通使用方式建立对象,填充数据
  35.     Permission p1 = new Permission();
  36.     p1.setName("1111");
  37.  
  38.     //2.持久化
  39.     s.save(p1);
  40.     //此时p1已经可以在数据库中找到
  41.     t.commit();
  42.     s.close();
  43.  
  44.   }
  45. }

 

抱歉!评论已关闭.