Hibernate annoation (one load)

Source: Internet
Author: User
Tags resource thread tostring

One, configuration file loading

1,configuration

If it is not annoation, you can use configuration configuration = new configuration ();

Using Annoation, you can use configuration configuration = new Annotationconfiguration ();

2, loading, using the Onfiguration configure method according to the method parameters can have a few load modes:

(1) Configure ();

Eating method will go to classpath under the search for our configuration file

In fact, the Configure ("/hibernate.cfg.xml") was invoked; the Configure (String resource);

(2) Configure (String Resource);

The most commonly used method of the square, in fact, calls the Doconfigure (InputStream stream, String resourcename);

Explain a little internal code:

Confighelper.getresourceasstream (Resource);

Java code

 String stripped = resource.startsWith("/") ? 
  resource.substring(1) : resource;
 
 InputStream stream = null;
 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 if (classLoader!=null) {
  stream = classLoader.getResourceAsStream( stripped );
 }
 if ( stream == null ) {
  stream = Environment.class.getResourceAsStream( resource );
 }
 if ( stream == null ) {
  stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
 }
 if ( stream == null ) {
  throw new HibernateException( resource + " not found" );
 }
 return stream;

The first line of high number we "/hibernate.cfg.xml" can remove the previous "/" which calls the Thread.CurrentThread (). Getcontextclassloader (); It's usually our appclassloader.

by stream = Environment.class.getResourceAsStream (resource); can see our "/ Hibernate.cfg.xml can also be placed in the same directory as and environment to our code in the use of (3), (4), (5) can be placed in the load class package or other

(3) Configure (URL URL);

Doconfigure (Url.openstream (), url.tostring ()); Call (5)

(4) Configure (File configfile);

Doconfigure (New FileInputStream (ConfigFile), configfile.tostring ()); Call (5)

(5) Doconfigure (InputStream stream, String resourcename);

Use DOM4J to parse the file as document and then

Xmlhelper.createsaxreader (resourcename, errors, Entityresolver)

. Read (new InputSource (stream));

Fall out of Use

(6) Configure (document document);

In this method, all configuration information and mapping class or HB files are parsed.

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.