溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

(02)Hibernate核心API

發(fā)布時(shí)間:2020-06-04 15:29:50 來源:網(wǎng)絡(luò) 閱讀:305 作者:lsieun 欄目:數(shù)據(jù)庫


1、Configuration

org.hibernate.cfg.Configuration是配置管理類對(duì)象。


1.1、configure()

加載主配置文件的方法(hibernate.cfg.xml),默認(rèn)加載src/hibernate.cfg.xml。



1.2、configure(String resource)

config.configure(“cn/config/hibernate.cfg.xml”);   加載指定路徑下指定名稱的主配置文件


1.3、buildSessionFactory()

創(chuàng)建session的工廠對(duì)象



Configuration部分源碼:

/**
 * An instance of <tt>Configuration</tt> allows the application
 * to specify properties and mapping documents to be used when
 * creating a <tt>SessionFactory</tt>. Usually an application will create
 * a single <tt>Configuration</tt>, build a single instance of
 * <tt>SessionFactory</tt> and then instantiate <tt>Session</tt>s in
 * threads servicing client requests. The <tt>Configuration</tt> is meant
 * only as an initialization-time object. <tt>SessionFactory</tt>s are
 * immutable and do not retain any association back to the
 * <tt>Configuration</tt>.<br>
 * <br>
 * A new <tt>Configuration</tt> will use the properties specified in
 * <tt>hibernate.properties</tt> by default.
 *
 * @author Gavin King
 * @see org.hibernate.SessionFactory
 */
public class Configuration implements Serializable {
	/**
	 * Use the mappings and properties specified in an application resource named <tt>hibernate.cfg.xml</tt>.
	 *
	 * @return this for method chaining
	 *
	 * @throws HibernateException Generally indicates we cannot find <tt>hibernate.cfg.xml</tt>
	 *
	 * @see #configure(String)
	 */
	public Configuration configure() throws HibernateException {
		configure( "/hibernate.cfg.xml" );
		return this;
	}

	/**
	 * Use the mappings and properties specified in the given application resource. The format of the resource is
	 * defined in <tt>hibernate-configuration-3.0.dtd</tt>.
	 * <p/>
	 * The resource is found via {@link #getConfigurationInputStream}
	 *
	 * @param resource The resource to use
	 *
	 * @return this for method chaining
	 *
	 * @throws HibernateException Generally indicates we cannot find the named resource
	 *
	 * @see #doConfigure(java.io.InputStream, String)
	 */
	public Configuration configure(String resource) throws HibernateException {
		log.info( "configuring from resource: " + resource );
		InputStream stream = getConfigurationInputStream( resource );
		return doConfigure( stream, resource );
	}

	/**
	 * Create a {@link SessionFactory} using the properties and mappings in this configuration. The
	 * {@link SessionFactory} will be immutable, so changes made to {@code this} {@link Configuration} after
	 * building the {@link SessionFactory} will not affect it.
	 *
	 * @return The build {@link SessionFactory}
	 *
	 * @throws HibernateException usually indicates an invalid configuration or invalid mapping information
	 */
	public SessionFactory buildSessionFactory() throws HibernateException {
		log.debug( "Preparing to build session factory with filters : " + filterDefinitions );

		secondPassCompile();
		if ( ! metadataSourceQueue.isEmpty() ) {
			log.warn( "mapping metadata cache was not completely processed" );
		}

		enableLegacyHibernateValidator();
		enableBeanValidation();
		enableHibernateSearch();

		validate();
		Environment.verifyProperties( properties );
		Properties copy = new Properties();
		copy.putAll( properties );
		PropertiesHelper.resolvePlaceHolders( copy );
		Settings settings = buildSettings( copy );

		return new SessionFactoryImpl(
				this,
				mapping,
				settings,
				getInitializedEventListeners(),
				sessionFactoryObserver
			);
	}

}


2、SessionFactory

org.hibernate.SessionFactory

session的工廠(或者說代表了這個(gè)hibernate.cfg.xml配置文件)


The main contract here is the creation of Session instances.  Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory.

SessionFactory是一個(gè)接口,它的主要職責(zé)就是創(chuàng)建Session實(shí)例。通常情況下,一個(gè)application只有一個(gè)單獨(dú)的SessionFactory實(shí)例,不同的線程從這個(gè)SessionFactory當(dāng)中獲取Session的實(shí)例。


The internal state of a SessionFactory is immutable.  Once it is created this internal state is set.  This internal state includes all of the metadata about Object/Relational Mapping.

SessionFactory的內(nèi)部狀態(tài)是不變以,也就是說,一旦SessionFactory被創(chuàng)建,它的內(nèi)部就不會(huì)再發(fā)生變化。


2.1、openSession()

創(chuàng)建一個(gè)sesison對(duì)象



2.2、getCurrentSession()

創(chuàng)建session或取出session對(duì)象



SessionFactory部分源碼:

/**
 * The main contract here is the creation of {@link Session} instances.  Usually
 * an application has a single {@link SessionFactory} instance and threads
 * servicing client requests obtain {@link Session} instances from this factory.
 * <p/>
 * The internal state of a {@link SessionFactory} is immutable.  Once it is created
 * this internal state is set.  This internal state includes all of the metadata
 * about Object/Relational Mapping.
 * <p/>
 * Implementors <strong>must</strong> be threadsafe.
 *
 * @see org.hibernate.cfg.Configuration
 *
 * @author Gavin King
 * @author Steve Ebersole
 */
public interface SessionFactory extends Referenceable, Serializable {

	/**
	 * Open a {@link Session}.
	 * <p/>
	 * JDBC {@link Connection connection(s} will be obtained from the
	 * configured {@link org.hibernate.connection.ConnectionProvider} as needed
	 * to perform requested work.
	 *
	 * @return The created session.
	 *
	 * @throws HibernateException Indicates a peroblem opening the session; pretty rare here.
	 */
	public org.hibernate.classic.Session openSession() throws HibernateException;

	/**
	 * Obtains the current session.  The definition of what exactly "current"
	 * means controlled by the {@link org.hibernate.context.CurrentSessionContext} impl configured
	 * for use.
	 * <p/>
	 * Note that for backwards compatibility, if a {@link org.hibernate.context.CurrentSessionContext}
	 * is not configured but a JTA {@link org.hibernate.transaction.TransactionManagerLookup}
	 * is configured this will default to the {@link org.hibernate.context.JTASessionContext}
	 * impl.
	 *
	 * @return The current session.
	 *
	 * @throws HibernateException Indicates an issue locating a suitable current session.
	 */
	public org.hibernate.classic.Session getCurrentSession() throws HibernateException;

}


注意:openSession()和getCurrentSession()的返回值是 org.hibernate.classic.Session類型。

org.hibernate.classic.Session定義如下:

org.hibernate.classic.Session定義如下:
/**
 * An extension of the <tt>Session</tt> API, including all
 * deprecated methods from Hibernate2. This interface is
 * provided to allow easier migration of existing applications.
 * New code should use <tt>org.hibernate.Session</tt>.
 * @author Gavin King
 */
public interface Session extends org.hibernate.Session {

}


org.hibernate.Session定義如下:

/**
 * The main runtime interface between a Java application and Hibernate. This is the
 * central API class abstracting the notion of a persistence service.<br>
 * <br>
 * The lifecycle of a <tt>Session</tt> is bounded by the beginning and end of a logical
 * transaction. (Long transactions might span several database transactions.)<br>
 * <br>
 * The main function of the <tt>Session</tt> is to offer create, read and delete operations
 * for instances of mapped entity classes. Instances may exist in one of three states:<br>
 * <br>
 * <i>transient:</i> never persistent, not associated with any <tt>Session</tt><br>
 * <i>persistent:</i> associated with a unique <tt>Session</tt><br>
 * <i>detached:</i> previously persistent, not associated with any <tt>Session</tt><br>
 * <br>
 * Transient instances may be made persistent by calling <tt>save()</tt>,
 * <tt>persist()</tt> or <tt>saveOrUpdate()</tt>. Persistent instances may be made transient
 * by calling<tt> delete()</tt>. Any instance returned by a <tt>get()</tt> or
 * <tt>load()</tt> method is persistent. Detached instances may be made persistent
 * by calling <tt>update()</tt>, <tt>saveOrUpdate()</tt>, <tt>lock()</tt> or <tt>replicate()</tt>. 
 * The state of a transient or detached instance may also be made persistent as a new
 * persistent instance by calling <tt>merge()</tt>.<br>
 * <br>
 * <tt>save()</tt> and <tt>persist()</tt> result in an SQL <tt>INSERT</tt>, <tt>delete()</tt>
 * in an SQL <tt>DELETE</tt> and <tt>update()</tt> or <tt>merge()</tt> in an SQL <tt>UPDATE</tt>. 
 * Changes to <i>persistent</i> instances are detected at flush time and also result in an SQL
 * <tt>UPDATE</tt>. <tt>saveOrUpdate()</tt> and <tt>replicate()</tt> result in either an
 * <tt>INSERT</tt> or an <tt>UPDATE</tt>.<br>
 * <br>
 * It is not intended that implementors be threadsafe. Instead each thread/transaction
 * should obtain its own instance from a <tt>SessionFactory</tt>.<br>
 * <br>
 * A <tt>Session</tt> instance is serializable if its persistent classes are serializable.<br>
 * <br>
 * A typical transaction should use the following idiom:
 * <pre>
 * Session sess = factory.openSession();
 * Transaction tx;
 * try {
 *     tx = sess.beginTransaction();
 *     //do some work
 *     ...
 *     tx.commit();
 * }
 * catch (Exception e) {
 *     if (tx!=null) tx.rollback();
 *     throw e;
 * }
 * finally {
 *     sess.close();
 * }
 * </pre>
 * <br>
 * If the <tt>Session</tt> throws an exception, the transaction must be rolled back
 * and the session discarded. The internal state of the <tt>Session</tt> might not
 * be consistent with the database after the exception occurs.
 *
 * @see SessionFactory
 * @author Gavin King
 */
public interface Session extends Serializable {

	/**
	 * Persist the given transient instance, first assigning a generated identifier. (Or
	 * using the current value of the identifier property if the <tt>assigned</tt>
	 * generator is used.) This operation cascades to associated instances if the
	 * association is mapped with <tt>cascade="save-update"</tt>.
	 *
	 * @param object a transient instance of a persistent class
	 * @return the generated identifier
	 * @throws HibernateException
	 */
	public Serializable save(Object object) throws HibernateException;

	/**
	 * Update the persistent instance with the identifier of the given detached
	 * instance. If there is a persistent instance with the same identifier,
	 * an exception is thrown. This operation cascades to associated instances
	 * if the association is mapped with <tt>cascade="save-update"</tt>.
	 *
	 * @param object a detached instance containing updated state
	 * @throws HibernateException
	 */
	public void update(Object object) throws HibernateException;

	/**
	 * Either {@link #save(Object)} or {@link #update(Object)} the given
	 * instance, depending upon resolution of the unsaved-value checks (see the
	 * manual for discussion of unsaved-value checking).
	 * <p/>
	 * This operation cascades to associated instances if the association is mapped
	 * with <tt>cascade="save-update"</tt>.
	 *
	 * @see Session#save(java.lang.Object)
	 * @see Session#update(Object object)
	 * @param object a transient or detached instance containing new or updated state
	 * @throws HibernateException
	 */
	public void saveOrUpdate(Object object) throws HibernateException;

	/**
	 * Return the persistent instance of the given entity class with the given identifier,
	 * or null if there is no such persistent instance. (If the instance is already associated
	 * with the session, return that instance. This method never returns an uninitialized instance.)
	 *
	 * @param clazz a persistent class
	 * @param id an identifier
	 * @return a persistent instance or null
	 * @throws HibernateException
	 */
	public Object get(Class clazz, Serializable id) throws HibernateException;

	/**
	 * Return the persistent instance of the given entity class with the given identifier,
	 * assuming that the instance exists. This method might return a proxied instance that
	 * is initialized on-demand, when a non-identifier method is accessed.
	 * <br><br>
	 * You should not use this method to determine if an instance exists (use <tt>get()</tt>
	 * instead). Use this only to retrieve an instance that you assume exists, where non-existence
	 * would be an actual error.
	 *
	 * @param theClass a persistent class
	 * @param id a valid identifier of an existing persistent instance of the class
	 * @return the persistent instance or proxy
	 * @throws HibernateException
	 */
	public Object load(Class theClass, Serializable id) throws HibernateException;

}





3、Session

org.hibernate.Session

session對(duì)象維護(hù)了一個(gè)連接(Connection), 代表了與數(shù)據(jù)庫連接的會(huì)話。

Hibernate最重要的對(duì)象: 只用使用hibernate與數(shù)據(jù)庫操作,都用到這個(gè)對(duì)象


3.1、事務(wù)

beginTransaction()開啟一個(gè)事務(wù); hibernate要求所有的與數(shù)據(jù)庫的操作必須有事務(wù)的環(huán)境,否則報(bào)錯(cuò)!


3.2、保存和更新


save(Object object) 保存一個(gè)對(duì)象


update(Object object) 更新一個(gè)對(duì)象


saveOrUpdate(Object object) 保存或者更新的方法:沒有設(shè)置主鍵,執(zhí)行保存;有設(shè)置主鍵,執(zhí)行更新操作; 如果設(shè)置主鍵不存在報(bào)錯(cuò)!


示例代碼:

package com.rk.hibernate.a_hello;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class App2
{
	private static SessionFactory sessionFactory;

	static
	{
		/*
		 * //1. 創(chuàng)建配置管理類對(duì)象 Configuration config = new Configuration(); // 加載配置文件
		 * (默認(rèn)加載src/hibernate.cfg.xml) config.configure(); //2.
		 * 根據(jù)加載的配置管理類對(duì)象,創(chuàng)建SessionFactory對(duì)象 sessionFactory =
		 * config.buildSessionFactory();
		 */

		// 創(chuàng)建SessionFactory對(duì)象
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}

	// 1. 保存對(duì)象
	@Test
	public void testSave()
	{
		// 對(duì)象
		Employee emp = new Employee();
		emp.setEmpName("張三");
		emp.setWorkDate(new Date());

		// 根據(jù)session的工廠,創(chuàng)建session對(duì)象
		Session session = sessionFactory.openSession();

		// 開啟事務(wù)
		Transaction transaction = session.beginTransaction();
		// -----執(zhí)行操作-----
		session.save(emp);

		// 提交事務(wù)
		transaction.commit();
		// 關(guān)閉
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}

	// 2. 更新對(duì)象
	@Test
	public void testUpdate()
	{
		// 對(duì)象
		Employee emp = new Employee();
		emp.setEmpId(20);
		emp.setEmpName("小明");

		// 創(chuàng)建session
		Session session = sessionFactory.openSession();
		// 開啟事務(wù)
		Transaction tx = session.beginTransaction();
		// 更新。如果不提供id,則報(bào)錯(cuò);如果提供的id不存在,也報(bào)錯(cuò)
		session.update(emp);
		// 提交事務(wù)
		tx.commit();
		// 關(guān)閉
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}

	// 3. 保存或更新對(duì)象
	@Test
	public void testSaveOrUpdate()
	{
		// 對(duì)象
		Employee emp = new Employee();
		emp.setEmpId(3);
		emp.setEmpName("小紅");

		// 創(chuàng)建session
		Session session = sessionFactory.openSession();
		// 開啟事務(wù)
		Transaction tx = session.beginTransaction();
		// -------執(zhí)行操作-------
		// 沒有設(shè)置主鍵,執(zhí)行保存。
		//有設(shè)置主鍵,執(zhí)行更新操作; 如果設(shè)置主鍵不存在,則報(bào)錯(cuò)!
		session.saveOrUpdate(emp);
		// 提交事務(wù)
		tx.commit();
		// 關(guān)閉
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}
}


3.3、主鍵查詢


get(Class clazz, Serializable id) 主鍵查詢

load(Class theClass, Serializable id) 主鍵查詢 (支持懶加載)


示例代碼:

	@Test
	public void testQuery()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		// get主鍵查詢
		Employee emp1 = (Employee) session.get(Employee.class, 2);
		System.out.println(emp1);
		// load主鍵查詢
		Employee emp2 = (Employee) session.load(Employee.class, 3);
		System.out.println(emp2);

		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}


3.4、HQL查詢

HQL查詢與SQL查詢區(qū)別:

SQL: (結(jié)構(gòu)化查詢語句)查詢的是表(table)以及字段(column);  不區(qū)分大小寫。

HQL: hibernate  query  language 即hibernate提供的面向?qū)ο蟮牟樵冋Z言,查詢的是對(duì)象以及對(duì)象的屬性(property),區(qū)分大小寫。


示例代碼:

	// HQL查詢 【適合有數(shù)據(jù)庫基礎(chǔ)的】
	@Test
	public void testHQL()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		// HQL查詢
		Query q = session.createQuery("From Employee where empId=2 or empId=3");
		List<Employee> list = q.list();
		System.out.println(list);

		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}



3.5、Criteria查詢

完全面向?qū)ο蟮牟樵?/p>


示例代碼:

	//QBC查詢  , query by criteria  完全面向?qū)ο蟮牟樵?	@Test
	public void testQBC()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		Criteria criteria = session.createCriteria(Employee.class);
		// 條件
		criteria.add(Restrictions.eq("empId", 2));
		List<Employee> list = criteria.list();
		System.out.println(list);

		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}


3.6、本地SQL查詢

復(fù)雜的查詢,就要使用原生態(tài)的sql查詢也可以,就是本地sql查詢的支持!缺點(diǎn): 不能跨數(shù)據(jù)庫平臺(tái)!


示例代碼:

	//SQL
	@Test
	public void testSQL()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		// 把每一行記錄封裝為對(duì)象數(shù)組,再添加到list集合
		//SQLQuery sqlQuery = session.createSQLQuery("select * from employee");
		
		// 把每一行記錄封裝為 指定的對(duì)象類型
		SQLQuery sqlQuery = session.createSQLQuery("select * from employee").addEntity(Employee.class);
		List<Employee> list = sqlQuery.list();
		System.out.println(list);
		
		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}


完整示例代碼:

package com.rk.hibernate.a_hello;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

public class App3
{
	private static SessionFactory sessionFactory;
	static
	{
		// 創(chuàng)建SessionFactory對(duì)象
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}

	@Test
	public void testQuery()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		// get主鍵查詢
		Employee emp1 = (Employee) session.get(Employee.class, 2);
		System.out.println(emp1);
		// load主鍵查詢
		Employee emp2 = (Employee) session.load(Employee.class, 3);
		System.out.println(emp2);

		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}

	// HQL查詢 【適合有數(shù)據(jù)庫基礎(chǔ)的】
	@Test
	public void testHQL()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		// HQL查詢
		Query q = session.createQuery("From Employee where empId=2 or empId=3");
		List<Employee> list = q.list();
		System.out.println(list);

		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}

	//QBC查詢  , query by criteria  完全面向?qū)ο蟮牟樵?	@Test
	public void testQBC()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		
		Criteria criteria = session.createCriteria(Employee.class);
		// 條件
		criteria.add(Restrictions.eq("empId", 2));
		List<Employee> list = criteria.list();
		System.out.println(list);

		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}

	//SQL
	@Test
	public void testSQL()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		// 把每一行記錄封裝為對(duì)象數(shù)組,再添加到list集合
		//SQLQuery sqlQuery = session.createSQLQuery("select * from employee");
		
		// 把每一行記錄封裝為 指定的對(duì)象類型
		SQLQuery sqlQuery = session.createSQLQuery("select * from employee").addEntity(Employee.class);
		List<Employee> list = sqlQuery.list();
		System.out.println(list);
		
		tx.commit();
		session.close();
		System.out.println("執(zhí)行結(jié)束!");
	}

}


4、Transaction

org.hibernate.Transaction是hibernate事務(wù)對(duì)象


A transaction is associated with a Session and is usually instantiated by a call to Session.beginTransaction().

transaction與Session相關(guān)聯(lián),通常由Session.beginTransaction()方法創(chuàng)建。


A single session might span multiple transactions since the notion of a session (a conversation between the application and the datastore) is of coarser granularity than the notion of a transaction. However, it is intended that there be at most one uncommitted Transaction associated with a particular Session at any time.

session可以理解為application和datastore之間一次conversation。在一個(gè)session產(chǎn)生多個(gè)transactions,但是在同一時(shí)間內(nèi),只只能有一個(gè)未提交的transaction。


package org.hibernate;

import javax.transaction.Synchronization;

/**
 * Allows the application to define units of work, while
 * maintaining abstraction from the underlying transaction
 * implementation (eg. JTA, JDBC).<br>
 * <br>
 * A transaction is associated with a <tt>Session</tt> and is
 * usually instantiated by a call to <tt>Session.beginTransaction()</tt>.
 * A single session might span multiple transactions since
 * the notion of a session (a conversation between the application
 * and the datastore) is of coarser granularity than the notion of
 * a transaction. However, it is intended that there be at most one
 * uncommitted <tt>Transaction</tt> associated with a particular
 * <tt>Session</tt> at any time.<br>
 * <br>
 * Implementors are not intended to be threadsafe.
 *
 * @see Session#beginTransaction()
 * @see org.hibernate.transaction.TransactionFactory
 * @author Anton van Straaten
 */
public interface Transaction {
	
	/**
	 * Begin a new transaction.
	 */
	public void begin() throws HibernateException;

	/**
	 * Flush the associated <tt>Session</tt> and end the unit of work (unless
	 * we are in {@link FlushMode#MANUAL}.
	 * </p>
	 * This method will commit the underlying transaction if and only
	 * if the underlying transaction was initiated by this object.
	 *
	 * @throws HibernateException
	 */
	public void commit() throws HibernateException;

	/**
	 * Force the underlying transaction to roll back.
	 *
	 * @throws HibernateException
	 */
	public void rollback() throws HibernateException;

	/**
	 * Was this transaction rolled back or set to rollback only?
	 * <p/>
	 * This only accounts for actions initiated from this local transaction.
	 * If, for example, the underlying transaction is forced to rollback via
	 * some other means, this method still reports false because the rollback
	 * was not initiated from here.
	 *
	 * @return boolean True if the transaction was rolled back via this
	 * local transaction; false otherwise.
	 * @throws HibernateException
	 */
	public boolean wasRolledBack() throws HibernateException;

	/**
	 * Check if this transaction was successfully committed.
	 * <p/>
	 * This method could return <tt>false</tt> even after successful invocation
	 * of {@link #commit}.  As an example, JTA based strategies no-op on
	 * {@link #commit} calls if they did not start the transaction; in that case,
	 * they also report {@link #wasCommitted} as false.
	 *
	 * @return boolean True if the transaction was (unequivocally) committed
	 * via this local transaction; false otherwise.
	 * @throws HibernateException
	 */
	public boolean wasCommitted() throws HibernateException;
	
	/**
	 * Is this transaction still active?
	 * <p/>
	 * Again, this only returns information in relation to the
	 * local transaction, not the actual underlying transaction.
	 *
	 * @return boolean Treu if this local transaction is still active.
	 */
	public boolean isActive() throws HibernateException;

	/**
	 * Register a user synchronization callback for this transaction.
	 *
	 * @param synchronization The Synchronization callback to register.
	 * @throws HibernateException
	 */
	public void registerSynchronization(Synchronization synchronization) 
	throws HibernateException;

	/**
	 * Set the transaction timeout for any transaction started by
	 * a subsequent call to <tt>begin()</tt> on this instance.
	 *
	 * @param seconds The number of seconds before a timeout.
	 */
	public void setTimeout(int seconds);
}




5、共性問題

(1)ClassNotFoundException…., 缺少jar文件!

(2)如果程序執(zhí)行程序,hibernate也有生成sql語句,但數(shù)據(jù)沒有結(jié)果影響。問題一般是事務(wù)忘記提交…….遇到問題,一定看錯(cuò)誤提示!


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI