當前位置: 首頁>>代碼示例>>Java>>正文


Java SessionFactory.openSession方法代碼示例

本文整理匯總了Java中org.hibernate.SessionFactory.openSession方法的典型用法代碼示例。如果您正苦於以下問題:Java SessionFactory.openSession方法的具體用法?Java SessionFactory.openSession怎麽用?Java SessionFactory.openSession使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.hibernate.SessionFactory的用法示例。


在下文中一共展示了SessionFactory.openSession方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: hibernate_with_active_span_only

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void hibernate_with_active_span_only() {
  SessionFactory sessionFactory = createSessionFactory(true);
  Session session = sessionFactory.openSession();

  Employee employee = new Employee();
  session.beginTransaction();
  session.save(employee);
  session.getTransaction().commit();
  session.close();
  sessionFactory.close();

  assertNotNull(employee.id);

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(0, finishedSpans.size());

  assertNull(mockTracer.activeSpan());
}
 
開發者ID:opentracing-contrib,項目名稱:java-jdbc,代碼行數:20,代碼來源:HibernateTest.java

示例2: hibernate

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void hibernate() throws InterruptedException {
  SessionFactory sessionFactory = createSessionFactory("");
  Session session = sessionFactory.openSession();

  Employee employee = new Employee();
  session.beginTransaction();
  session.save(employee);
  session.getTransaction().commit();
  session.close();
  sessionFactory.close();

  assertNotNull(employee.id);

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(8, finishedSpans.size());
  checkTags(finishedSpans, "myservice", "jdbc:hsqldb:mem:hibernate");
  assertNull(mockTracer.scopeManager().active());
}
 
開發者ID:opentracing-contrib,項目名稱:java-p6spy,代碼行數:20,代碼來源:HibernateTest.java

示例3: withPeerNameInUrl

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void withPeerNameInUrl() throws InterruptedException {
  SessionFactory sessionFactory = createSessionFactory(";tracingPeerService=inurl");
  Session session = sessionFactory.openSession();

  Employee employee = new Employee();
  session.beginTransaction();
  session.save(employee);
  session.getTransaction().commit();
  session.close();
  sessionFactory.close();

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(8, finishedSpans.size());

  checkTags(finishedSpans, "inurl", "jdbc:hsqldb:mem:hibernate;tracingPeerService=inurl");

  assertNull(mockTracer.scopeManager().active());
}
 
開發者ID:opentracing-contrib,項目名稱:java-p6spy,代碼行數:20,代碼來源:HibernateTest.java

示例4: withActiveSpanOnlyNoParent

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void withActiveSpanOnlyNoParent() throws InterruptedException {
  SessionFactory sessionFactory = createSessionFactory(";traceWithActiveSpanOnly=true");
  Session session = sessionFactory.openSession();

  Employee employee = new Employee();
  session.beginTransaction();
  session.save(employee);
  session.getTransaction().commit();
  session.close();
  sessionFactory.close();

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(0, finishedSpans.size());

  assertNull(mockTracer.scopeManager().active());
}
 
開發者ID:opentracing-contrib,項目名稱:java-p6spy,代碼行數:18,代碼來源:HibernateTest.java

示例5: testSaveAdmin2

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void testSaveAdmin2(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	Admin admin = new Admin();
	admin.setName("cairou");
	admin.setUsername("admin");
	admin.setPwd("admin");
	Authorization authorization = new Authorization();
	authorization.setSuperSet(1);
	authorization.setAdmin(admin);
	admin.setAuthorization(authorization);
	session.save(admin);
	transaction.commit();
	session.close();
}
 
開發者ID:cckevincyh,項目名稱:LibrarySystem,代碼行數:18,代碼來源:TestAdmin.java

示例6: main

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
public static void main(String[] args) {
	Configuration cfg=null;
	SessionFactory factory=null;
	Session ses=null;
	 Transaction tx=null;
			cfg=new Configuration().configure("com/app/cfgs/hibernate.cfg.xml");
			factory=cfg.buildSessionFactory();
		ses=factory.openSession();
	
		tx=ses.beginTransaction();
		 String hql="update bigbazarModel set item_price=:price where bazarid=:id";
		 Query q=ses.createQuery(hql);
		 		q.setParameter("id", 1002);
		 		q.setParameter("price", 60.0f);
		 			int c=q.executeUpdate();
		 			//int count=Integer.parseUnsignedInt(c);
		 			tx.commit();
		 			System.out.println("\t\t"+c+" rows Updated");
		 		
		 			factory.close();

}
 
開發者ID:pratikdimble,項目名稱:Hibernate_HQL_UniqueResult_ExecuteUpdate_CopyData_Delete_Update,代碼行數:23,代碼來源:update_HQL.java

示例7: queryForInt

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
private int queryForInt(String hql,Object[] parameters,Map<String,Object> parameterMap,String dataSourceName){
	SessionFactory factory=this.getSessionFactory(dataSourceName);
	Session session=factory.openSession();
	int count=0;
	try{
		Query countQuery=session.createQuery(hql);
		if(parameters!=null){
			setQueryParameters(countQuery,parameters);				
		}else if(parameterMap!=null){
			setQueryParameters(countQuery,parameterMap);								
		}
		Object countObj=countQuery.uniqueResult();
		if(countObj instanceof Long){
			count=((Long)countObj).intValue();
		}else if(countObj instanceof Integer){
			count=((Integer)countObj).intValue();
		}
	}finally{
		session.close();
	}
	return count;
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:23,代碼來源:HibernateDao.java

示例8: testFindBook

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void testFindBook(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	String hql= "from Book";
	List createQuery = session.createQuery(hql).list();
	Book book = (Book) createQuery.get(0);
	System.out.println(book);
	System.out.println(book.getBookType());
}
 
開發者ID:cckevincyh,項目名稱:LibrarySystem,代碼行數:11,代碼來源:TestBook.java

示例9: testGetAdmin3

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void testGetAdmin3(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Authorization authorization = (Authorization) session.get(Authorization.class, 1);
	System.out.println(authorization.getAdmin().getName());
	session.close();
}
 
開發者ID:cckevincyh,項目名稱:LibrarySystem,代碼行數:9,代碼來源:TestAdmin.java

示例10: testSaveBack

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void testSaveBack(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	BackInfo backInfo = new BackInfo();
	BorrowInfo borrowInfo = new BorrowInfo();
	borrowInfo.setBorrowId(1);
	backInfo.setBorrowInfo(borrowInfo);
	backInfo.setBorrowId(1);
	session.save(backInfo);
	transaction.commit();
	session.close();
}
 
開發者ID:cckevincyh,項目名稱:LibrarySystem,代碼行數:15,代碼來源:TestBack.java

示例11: testDeleteReader

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Test
public void testDeleteReader(){
	SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	Reader reader = (Reader) session.get(Reader.class, 1);
	session.delete(reader);
	transaction.commit();
	session.close();
}
 
開發者ID:cckevincyh,項目名稱:LibrarySystem,代碼行數:11,代碼來源:TestReader.java

示例12: handleRequest

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
@Override
public String handleRequest(Request request, Context context) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    try (Session session = sessionFactory.openSession()) {
        session.beginTransaction();
        Employee employee = new Employee();
        employee.setId(request.id);
        employee.setName(request.name);
        session.save(employee);
        session.getTransaction().commit();
    }

    return String.format("Added %s %s.", request.id, request.name);
}
 
開發者ID:arun-gupta,項目名稱:lambda-rds-mysql,代碼行數:15,代碼來源:EmployeeHandler.java

示例13: runInTransaction

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
protected void runInTransaction(SessionFactory factory, HibernateCall call) throws Exception
{
	Transaction trans = null;
	Session session = null;
	try
	{
		session = factory.openSession();
		trans = session.beginTransaction();
		call.run(session);
		trans.commit();
	}
	catch( Exception t )
	{
		if( trans != null )
		{
			trans.rollback();
		}
		Throwables.propagate(t);
	}
	finally
	{
		if( session != null )
		{
			session.close();
		}
	}
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:28,代碼來源:AbstractHibernateMigration.java

示例14: openSession

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
/**
 * Open a Session for the SessionFactory that this filter uses.
 * <p>The default implementation delegates to the {@link SessionFactory#openSession}
 * method and sets the {@link Session}'s flush mode to "MANUAL".
 * @param sessionFactory the SessionFactory that this filter uses
 * @return the Session to use
 * @throws DataAccessResourceFailureException if the Session could not be created
 * @see org.hibernate.FlushMode#MANUAL
 */
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
	try {
		Session session = sessionFactory.openSession();
		session.setFlushMode(FlushMode.MANUAL);
		return session;
	}
	catch (HibernateException ex) {
		throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:20,代碼來源:OpenSessionInViewFilter.java

示例15: getNewSession

import org.hibernate.SessionFactory; //導入方法依賴的package包/類
/**
 * Get a new Hibernate Session from the given SessionFactory.
 * Will return a new Session even if there already is a pre-bound
 * Session for the given SessionFactory.
 * <p>Within a transaction, this method will create a new Session
 * that shares the transaction's JDBC Connection. More specifically,
 * it will use the same JDBC Connection as the pre-bound Hibernate Session.
 * @param sessionFactory Hibernate SessionFactory to create the session with
 * @param entityInterceptor Hibernate entity interceptor, or {@code null} if none
 * @return the new Session
 */
@SuppressWarnings("deprecation")
public static Session getNewSession(SessionFactory sessionFactory, Interceptor entityInterceptor) {
	Assert.notNull(sessionFactory, "No SessionFactory specified");

	try {
		SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
		if (sessionHolder != null && !sessionHolder.isEmpty()) {
			if (entityInterceptor != null) {
				return sessionFactory.openSession(sessionHolder.getAnySession().connection(), entityInterceptor);
			}
			else {
				return sessionFactory.openSession(sessionHolder.getAnySession().connection());
			}
		}
		else {
			if (entityInterceptor != null) {
				return sessionFactory.openSession(entityInterceptor);
			}
			else {
				return sessionFactory.openSession();
			}
		}
	}
	catch (HibernateException ex) {
		throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:39,代碼來源:SessionFactoryUtils.java


注:本文中的org.hibernate.SessionFactory.openSession方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。