本文整理汇总了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());
}
示例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());
}
示例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());
}
示例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());
}
示例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();
}
示例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;
}
示例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());
}
示例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();
}
示例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();
}
示例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();
}
示例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);
}
示例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();
}
}
}
示例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);
}
}
示例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);
}
}