Example of using the Weblogic data source as the hibernate Data Source

Source: Internet
Author: User
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.
  1. PackageCom. jarie. Business. Organization;
  2.  
  3. ImportJava. Io.Serializable;
  4.  
  5. /**
  6. * <P> title: </P>
  7. * <P> Description: Permission </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 ClassPermissionImplements Serializable{
  15. Private StringID;// PK
  16. Private StringName;// Name
  17. Private StringDescription;// Description
  18. Private StringModule;// Module ID
  19. Private StringPower;// Weight, for example, browse $ add $ Delete $ change
  20. Private IntScope;// Range, 0: myself, 1: This unit, 2: All units
  21. Public Static VoidMain (String[] ARGs ){
  22. }
  23. Public StringGETID (){
  24. ReturnID;
  25. }
  26. Public VoidSetid (StringID ){
  27. This. ID = ID;
  28. }
  29. Public StringGetname (){
  30. ReturnName;
  31. }
  32. Public VoidSetname (StringName ){
  33. This. Name = Name;
  34. }
  35. Public StringGetdescription (){
  36. ReturnDescription;
  37. }
  38. Public VoidSetdescription (StringDescription ){
  39. This. Description = description;
  40. }
  41. Public StringGetmodule (){
  42. ReturnModule;
  43. }
  44. Public VoidSetmodule (StringModule ){
  45. This. Module = module;
  46. }
  47. Public StringGetpower (){
  48. ReturnPower;
  49. }
  50. Public VoidSetpower (StringPower ){
  51. This. Power = power;
  52. }
  53. Public IntGetscope (){
  54. ReturnScope;
  55. }
  56. Public VoidSetscope (IntScope ){
  57. This. Scope = scope;
  58. }
  59. }
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.

  1. PackageCom. jarie. Business. Organization;
  2.  
  3. ImportNet. SF. hibernate. Session;
  4. ImportNet. SF. hibernate. transaction;
  5. ImportNet. SF. hibernate. sessionfactory;
  6. ImportNet. SF. hibernate. cfg. configuration;
  7. ImportNet. SF. hibernate. tool. hbm2ddl. schemaexport;
  8.  
  9. ImportJavax. naming.Initialcontext;
  10. ImportJavax. naming.Context;
  11. ImportJavax. SQL .*;
  12. ImportJava. SQL .*;
  13. ImportJava. util .*;
  14. ImportCom. jarie. utils. J2EE .*;
  15.  
  16. Public ClassTest {
  17. Private StaticSessionfactory sessions;
  18.  
  19. Public Static VoidMain (String[] ARGs)Throws Exception{
  20. Configuration conf =NewConfiguration (). addclass (permission.Class);
  21. Sessions = Conf. buildsessionfactory ();
  22. // Generate and output SQL statements to files (current directory) and databases
  23. Schemaexport dbexport =NewSchemaexport (CONF );
  24. Dbexport. setoutputfile ("SQL .txt ");
  25. Dbexport. Create (True,True);
  26.  
  27.  
  28. // Start ......
  29. Session S = sessions. opensession ();
  30. Transaction T = S. begintransaction ();
  31.  
  32. // 1. Create an object and fill in data in common usage
  33. Permission p1 =NewPermission ();
  34. P1.setname ("1111 ");
  35.  
  36. // 2. Persistence
  37. S. Save (P1 );
  38. // At this time, P1 can be found in the database
  39. T. Commit ();
  40. S. Close ();
  41.  
  42. }
  43. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.