Translation Page 41-48 First, two options are rarely used in addition to rapid testing and prototyping, most applications require a fixed profile. The file hibernate.properties and hibernate.cfg.xml provide the same features of Hibernate. Select which file to rely on your syntax hobby. As you will have to see in this chapter, it is also possible to mix the two options and use different settings for development and configuration. A rare user choice is to allow applications to provide a JDBC connection when you open a Hibernate session from a session factory (for example by calling sessions.opensession (myConnection). Using this choice means you don't have to specify any database connection properties. We do not recommend this method for new applications because they can be configured using the underlying configuration of database connections in the environment (eg, JDBC connection pools or application server data sources). In all configuration options, the setting of the database connection is the most important. They are different in management and non-management environments, so we have handled these two situations independently. Let us first start from the non-management environment. 2.3.2 Configuration in a non-management environment, such as a servlet container, is responsible for obtaining a JDBC connection. Hibernate is part of the application, so it will be responsible for obtaining these connections. But you need to tell Hibernate how to get (or create) JDBC connections. Typically, every time you create a connection to a database is not available. Instead, Java applications should use the JDBC connection pool. This has three reasons: ■ The cost of getting new connections is very expensive. ■ Maintaining many useless connections is also very wasteful. ■ Creating a pre-compilation statement is also expensive for many drivers. Figure 2.2 shows the role in the JDBC connection pool in the web application running environment. Because the non-managed environment does not implement a connection pool, the application must implement its own poolization algorithm or depend on the third-party library, such as an open source C3P0 connection pool. When using Hibernate, application code usually calls the connection pool to get JDBC connections and executes SQL statements. (Figure 2.2) When using hibernate, the image has changed: it is shown as a customer connecting pool as a JDBC, as shown in Figure 2.3. Application Code uses the Hibernate session and query API for continuous operation and then only needs to use the Hibernate Transaction API to manage database transactions. (Figure 2.3) Use the connection pool Hibernate to define a plug-in architecture, allowing you to integrate with any connection pool. However, Hibernate has built-in support for C3P0, so we will use it. Hibernate uses specific properties to create a connection pool. An example of a Hibernate.Properties file using C3P0 See Listing 2.4.
hibernate.connection.driver_class = org.postgresql.Driverhibernate.connection.url = jdbc: postgresql: //localhost/auctiondbhibernate.connection.username = auctionuserhibernate.connection.password = secrethibernate.dialect = net.sf.hibernate.dialect.PostgreSQLDialecthibernate. c3p0.min_size = 5hibernate.c3p0.max_size = 20hibernate.c3p0.timeout = 300hibernate.c3p0.max_statements = 50hibernate.c3p0.idle_test_period = 3000 (Listing 2.4) starting from the first row, the above code specify the following information: ■ implemented The name of the JAVA class of the JDBC driver (the driver's JAR file must be in the application ClassPath). ■ Specify the host name of the JDBC connection and the JDBC URL of the database name. ■ Database User Name. ■ Specify the user's database password. ■ A database dialect. Despite the efforts of ANSI standardization, different database vendors still have different SQL implementations. Therefore, you must specify a dialect. Hibernate contains built-in support for all popular SQL databases, and it is also easy to define new dialects. ■ The minimum value of the JDBC connection held by C3P0. ■ The maximum of the connections in the pool. If this value is exhausted at runtime, an exception will be thrown. ■ A timeout (in this example, it is 5 minutes or 300 seconds), and the idle connection is removed from the pool after this. ■ The maximum number of precompiled statements that can be cached. Caches the pre-compiled statements are high performance. Hibernate is required. ■ The idle time (in seconds) of the connection before the connection is active. The property specified by hibernate.c3p0. * Selects C3P0 as the Hibernate connection pool (in order to support C3P0, you do not need to set any other options). C3P0 has more features displayed in the previous example, so we recommend that you query the Hibernate API document. The Javadoc documentation of class net.sf.hibernate.cfg.Environment has all configure properties of Hibernate, including all C3P0-related settings and other Hibernate supported third-party connecting pool settings. Other supported connecting pools are apache dbcp and proxool. Before you decide, you should first try each connection pool in your environment. The Hibernate community is more inclined to C3P0 and Proxool. Hibernate also includes a default connection pool mechanism. This connection pool is only available for test or test hibernate. In the product system, you should not use this built-in pool. It is not expandable in the environment where concurrent requests is not expanded, and it lacks some professional connection pools to have fault tolerance. Start Hibernate how to use these properties to start Hibernate? You declare these properties in a file called hibernate.properties, so you only need to put this file in the application's classpath. When you create a configuration object, Hibernate is initialized for the first time, it will be automatically detected and read. Let us summarize the configuration steps you have learned so far (if you want to continue in a non-configured environment, this is a good time to download and install Hibernate). 1. Download and unpack your database's JDBC driver, which can usually be obtained from the Web site of the database vendor.
Place the JAR file in the application's classpath; also put hibernate2.jar in the same location. 2. Place the Hibernate's dependencies into classpath; they are posted with Hibernate in the lib / directory. At the same time, look at the text file readme.txt in the lib / directory, which contains a list of required and optional libraries. 3. Select a JDBC connection pool that hibernate support and configure it using the properties file. Don't forget to specify SQL dialects. 4. Let the configuration object know these properties by placing them in a hibernate.properties file in the ClassPath. 5. Create an instance of a configuration object in your application and load an XML mapping file using the AddResource () or AddClass () method. Create a session factory by calling the buildsessionFactory () method of the configuration object. Unfortunately, you have no mapping files yet. If you like, you can run an example of "Hello World" or skip the remaining parts of this chapter and start learning Chapter 3 to continue class and mapping. Or, if you want to know more about using Hibernate in the management environment, please continue to read. 2.3.3 Configuring a specific "cross-cut" relationship in the management environment, such as application security (authorization and verification), connect pool and transaction management. J2EE Application Server is a typical management environment. Although the application server is typically designed to support EJB, you can still use other services that you provide even if you don't use the EJB entity Bean. Hibernate is often used with session or message driver EJB, as shown in Figure 2.4. Like Servlet, JSP, and separate applications, EJB calls the same Hibernate API: Session (session), Transaction, and Query. The code related to Hibernate is fully portable between non-management and management environments. Hibernate has transparently handles different connection and transaction strategies. (Figure 2.4) The application server displays the connection pool to the JNDI binding data source, which is an instance of the Javax.jdbc.DataSource class. You need to provide a JNDI list to tell Hibernate, where to find the JNDI data source. An example of a Hibernate configuration file in this case See Listing 2.5. hibernate.connection.datasource = java: /comp/env/jdbc/AuctionDBhibernate.transaction.factory_class = net.sf.hibernate.transaction.JTATransactionFactoryhibernate.transaction.manager_lookup_class = net.sf.hibernate.transaction.JBossTransactionManagerLookuphibernate.dialect = net.sf .Hibernate.Dialev.postgreSQLDIALECT (Listing 2.5) This file first gives the JNDI name of the data source. The data source must be configured in the configuration descriptor for J2EE enterprise applications; this is a specific settings for vendors. Then you allow Hibernate to integrate with JTA. In order to fully integrate with the container transaction, Hibernate needs to locate the transaction manager of the application server. The J2EE specification does not define a standard method, but Hibernate contains support for all popular application servers. Of course, I will finally need to specify the SQL dialect.
Now you have been properly configured, using Hibernate in the management environment and not much difference in non-managed environments: all use mappings to create a configuration object and construct a session factory. However, many of the settings related to the environment is worth making additional considerations. Java has a standard transaction API: JTA, which is used to control transactions in the J2EE management environment. This is called a container management transaction (CMT). If there is a JTA transaction manager, the JDBC connection will be supported and fully under its control. This is different from the non-managed environment, using (or connecting pools) in a non-management environment directly manages JDBC connection and JDBC transactions. Therefore, the management environment and non-managed environment can use different transaction methods. Because hibernate needs to ensure portability between these two environments, it defines a set of APIs for controlling transactions. Hibernate's transaction interface abstract the lower JTA or JDBC transaction (or even potential CORBA transactions). This underlying transaction policy can be set to use attribute hibernate. Connection.Factory_class, which can remove one of the column two values: ■ Net.sf.hibernate.Transaction.jdbctransActionFactory represents a direct JDBC transaction. Such strategies should be used with connecting pools in a non-managed environment, and if it is not specified, it is the default. ■ Net.sf.hibernate.Transaction.jtatransactionFactory represents JTA. For CMT this is the correct policy, where the connection is supported by JTA. Note If you have a JTA transaction when you call BeGintractions (), the subsequent work will happen in the context of that transaction (otherwise, a new JTA transaction will begin). For more detailed introduction to the Hibernate transaction API, please refer to Chapter 5.1 "Transaction" for specific application scenarios. You only need to remember the two steps necessary to use the J2EE server: Set the factory class as the Hibernate Transaction API as previously described to support JTA, and declare your application server-specific transaction manager's lookup policy. It is necessary to find the strategy when you use Hibernate's second-level cache system, but even if you don't use the cache. Hibernate and Tomcat Tomcat are not a complete application server; it is just a servlet container, although it contains some features that are usually only available in the application server. One of the features may be used by Hibernate: Tomcat's connection pool. Tomcat is used inside the DBCP connection pool, but it is also displayed as a JNDI data source like a true application server. To configure the Tomcat data source, you need to edit the Server.xml file according to the guidance of the Tomcat JNDI / JDBC document. You can set hibernate.connection.DataSource to configure Hibernate to use this data source. Note that Tomcat does not include a transaction manager, so this situation is more like a non-managed environment previously described. Whether it is a simple servlet container or an application server, now you should have a hibernate system that is running. Create and compile a continuous class (for example, the original Message class), copy Hibernate and the class library it needs and the file hibernate.properties to ClassPath, construct a session factory. The next section contains advanced Hibernate configuration options. Many of them are recommended, such as writing an executable SQL statement to LOG to facilitate debugging; or using a convenient XML configuration file rather than unformatted attributes.
However, you can safely skip this section and go directly to the third chapter, and then come back after you learn more about continuous class. 2.4 Advanced Configuration Settings When you eventually have a Hibernate app that can run, there is a comprehensive understanding of all configuration parameters of Hibernate. These parameters allow you to optimize the behavior of Hibernate runtime, especially adjustments to JDBC (for example, when using JDBC for batch updates). Now we won't make you boring about these details; the best source of information about configuration options is of course Hibernate's reference documentation. In the previous section, we have explained that the options you need to know at the beginning. However, there is a parameter we must emphasize here. Whenever you use Hibernate development software, it will be used frequently. Set the property hibernate.show_sql to True to record all generated SQL on the console. You can use it to diagnose faults, adjust performance or just look at what is generated. It helps you know what the ORM layer is doing - this is why ORM does not hide SQL for developers. So far, we have been assumed to specify configuration parameters using the hibernate.properties file or using an instance of java.util.properties in the program. In addition, there may be a third option you might like: use the XML configuration file. 2.4.1 Using XML-based configuration You can use an XML configuration file (as shown in Listing 2.6) to completely configure a session factory. Unlike file hibernate.properties that only contain configuration parameters, file hibernate.cfg.xml can also specify the location of the mapping file. Many users prefer to focus on Hibernate in this way, rather than adding parameters to the configuration object in the application code.
XML Version = '1.0'Encoding =' UTF-8 '?>