Hibernate is the simplest configuration and use

xiaoxiao2021-03-06  43

1. Hibernate configuration

Hibernate gets configuring and creating sessionFactory via net.sf.hibernate.cfg.configuration. Once a sessionFactory is created, any change in Configutation works for SessionFactory, and sessionFactory cannot be changed. Get a variety of ways, hibernate.properties, hibernate.cfg.xml, can use the XML file using the Configuration class directly using the CONFIGURATION class. For web apps, you will be accessed as soon as you put it in Web-INF / CLASSES. It is also possible to initialize the configuration with a parameter constructor.

2. Get sessionFactory

SESSIONFACTORY sessions = cfg.buildsessionFactory ();

3. Create session

Create a session and need to be provided to the sessionFactory: java.sql.connection conn. There are two ways to provide JDBC connections.

A: Offers CONN

Java.sql.connection conn = DataSource.getConnection ();

Session session = sessions.opensession (conn);

B: JDBC connection provided by Hibernate

Configure them in the following ways and then use session session = sessions.openSession (); it is possible.

Pass a java.util.properties to configure.setproperties () method.

Provide hibernate.properties files in the root of ClassPath.

Specify the use system properties via java -dproperty = value.

In the hibernate.cfg.xml file, you contain elements.

Or bind to JNDI. This configuration has a corresponding XML version, using XML to configure easier. Configure the Hibernate documentation.

SESSIONFACTORY SF = New Configuration (). Configure (). BuildSessionFactory ();

or

SessionFactory sf = new configuration (). Configure ("catdb.cfg.xml"). BuildSessionFactory ();

转载请注明原文地址:https://www.9cbs.com/read-55000.html

New Post(0)