|
Persistent objects can have the following three states:
Temporary state (Transient): object temporary state, then the database does not have information about that object in the database before saving, if not persistent, the program exits the temporary state of object information will be lost. Any time may be garbage collected (not in the corresponding record in the database, should be new initialization), and after the execution save () method becomes Persistent objects (persistent objects), not included in the session management, an object in memory, no ID, no cache
Persistent state (Persistent): object persistent state, this time the database has information about the object when after saving into the database or from the database load, and not out of the Session. Because still Session, the persistent state of the object can do anything about the database, the database has a corresponding record exists to include session management. Clean up cache (dirty checking) time, will and database synchronization. There are memory, the cache there, the database has (ID)
Separated state (Detached): For the isolated state is the object had persistent state, but has now left the Session. Although the objects are separated state id value, but has been unable to perform the operation of the relevant database. For example, reading a collection of property lazy loading, you may be thrown LazyInitalizeException.
Profiles
Hibernate also supports dynamic configuration
XML configuration
Configurationcfg = new Configuration () addResource ( "com / clf / Cat.hbm.xml").;
// Annotation configuration
Configuration cfg = new Configuration () addClass (com.clf.bean.cat.Class).;
By setProperty ( "hibernate.dialect", "org.hibernate.dialect.MySQLDialect") similar approach can be dynamically set Hibernate parameters can also be used addProperties (Propertiesp) batch add parameters
General Configuration
Hibernate configuration file can be XML or properties files, the default configuration file name hibernate.cfg.xml or hibernate.properties, located classpath below. properties parameters in the file is a hibernate prefix, but no xml file
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc: mysql: // localhost:? 3306 / hibernate characterEncoding = UTF-8
hibernate.connection.username = root
hibernate.connection.password = admin
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.hbm2ddl.auto = create
hibernate.current_session_context_class = thread
< ? Xml version = "1.0" encoding = "UTF-8"?>
< ! DOCTYPE hibernate-configuration PUBLIC
"- // Hibernate / Hibernate Configuration DTD 3.0 // EN"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
< Hibernate-configuration>
< Session-factory>
< ! - Hibernate is true that will be sent to the database sql displayed ->
< Property name = "show_sql"> true < / property>
< -! SQL dialect, is set here MySQL ->
< Property name = "dialect"> net.sf.hibernate.dialect.MySQLDialect < / property>
< ! - A number of records in the database read ->
< Property name = "jdbc.fetch_size"> 50 < / property>
< ! - Set the database to delete in bulk ->
< Property name = "jdbc.batch_size"> 30 < / property>
< ! - Driver ->
< Property name = "connection.driver_class"> com.mysql.jdbc.Driver < / property>
< -! JDBC URL ->
< Property name = "connection.url"> jdbc: mysql:? // Localhost / dbname characterEncoding = gb2312 < / property>
< ! - Database user name ->
< Property name = "connection.username"> root < / property>
< ! - Database password ->
< Property name = "connection.password"> root < / property>
< ! - Mapping files ->
< Mapping resource = "com / amigo / pojo / User.hbm.xml" />
< Mapping resource = "com / amigo / pojo / Org.hbm.xml" />
< / Session-factory>
< / Hibernate-configuration>
Configure Data Source
< Session-factory>
< ! - The following is JNDI configuration ->
< ! - Name of data source ->
< Property name = "connection.datasource"> java: comp / env / jdbc / datasourcename < / property>
< ! - Data source provider ->
< Property name = "hibernate.jndi.url"> < / property>
< ! - Data source implementation class ->
< Property name = "hibernate.jndi.class"> < / property>
< -! Hibernate connection load class ->
< Property name = "connection.provider_class"> net.sf.hibernate.connection.DatasourceConnectionProvider < / property>
< Property name = "dialect"> net.sf.hibernate.dialect.SQLServerDialect < / property>
< ! - Mapping files ->
< Mapping resource = "com / amigo / pojo / User.hbm.xml" />
< Mapping resource = "com / amigo / pojo / Org.hbm.xml" />
< / Session-factory>
c3p0 connection pool
c3p0 Connection pooling is recommended to use Hibernate connection pool, if the need to use the connection pool, you need to c3p0 jar package was added to the classpath. Configuration example c3p0 connection pool as follows:
< Session-factory>
< Property name = "connection.driver_class"> ...... < / property>
< -! JDBC URL ->
< Property name = "connection.url"> ...... < / property>
< ! - Database user name ->
< Property name = "connection.username"> user < / property>
< ! - Database password ->
< Property name = "connection.password"> pass < / property>
< Property name = "c3p0.min_size"> 5 < / property>
< Property name = "c3p0.max_size"> 20 < / property>
< Property name = "c3p0.timeout"> 1800 < / property>
< Property name = "c3p0.max_statements"> 50 < / property>
......
< / Session-factory>
In the above configuration, Hibernate generates the connection according to the profile, and then to the c3p0 management. |
|
|
|