当前位置: 首页>>代码示例>>Java>>正文


Java Session.get方法代码示例

本文整理汇总了Java中org.hibernate.Session.get方法的典型用法代码示例。如果您正苦于以下问题:Java Session.get方法的具体用法?Java Session.get怎么用?Java Session.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.hibernate.Session的用法示例。


在下文中一共展示了Session.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findCategoryById

import org.hibernate.Session; //导入方法依赖的package包/类
/**
 * 根据类别id找到类别对象
 */
@Override
public Category findCategoryById(int category_id) {
	Category category = null;
	Session session = HibernateUtils.getSession();// 生成Session实例
	Transaction tx = session.beginTransaction();// 生成事务实例
	try {
		category = (Category) session.get(Category.class, category_id);
		tx.commit();// 提交事务
		// 调用session的get()方法,找到此用户到内存中
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback();// 事务回滚
	} finally {
		HibernateUtils.closeSession();// 关闭session实例
	}
	return category;
}
 
开发者ID:codekongs,项目名称:ImageClassify,代码行数:21,代码来源:CategoryService.java

示例2: findOauthByOauthId

import org.hibernate.Session; //导入方法依赖的package包/类
/**
 * 依据oauth_id找到Oauth对象并返回此对象
 */
@Override
public Oauth findOauthByOauthId(int oauth_id) {
	 Oauth oauth = null;
	 Session session = HibernateUtils.getSession();//生成Session实例
	 Transaction tx = session.beginTransaction();//生成事务实例
	 try {
	     oauth =  (Oauth) session.get(Oauth.class, oauth_id);
	     //调用session的get()方法,找到此用户到内存中
	    tx.commit();//提交事务
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback();//事务回滚
	}finally{
		HibernateUtils.closeSession();//关闭session实例
	}
	 return oauth;
}
 
开发者ID:codekongs,项目名称:ImageClassify,代码行数:21,代码来源:OauthService.java

示例3: getVariable

import org.hibernate.Session; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private Variable getVariable(Context context,ProcessInstance pi) {
	Session session=context.getSession();
	Criteria criteria=session.createCriteria(Variable.class).add(Restrictions.eq("processInstanceId", pi.getId())).add(Restrictions.eq("key", key));;
	List<Variable> vars=criteria.list();
	if(vars.size()==0){
		if(pi.getParentId()>0){
			ProcessInstance parentInstance=(ProcessInstance)session.get(ProcessInstance.class,pi.getParentId());
			return getVariable(context, parentInstance);
		}else{
			return null;
		}
	}else{
		for(Variable var:vars){
			if(var instanceof BlobVariable){
				((BlobVariable)var).initValue(context);
			}
			if(var instanceof TextVariable){
				((TextVariable)var).initValue(context);
			}
		}
		return vars.get(0);
	}
}
 
开发者ID:youseries,项目名称:uflo,代码行数:25,代码来源:GetProcessInstanceVariableCommand.java

示例4: getById

import org.hibernate.Session; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public T getById(Long pk) throws SQLException {
	Session session = HibernateUtil.getSessionFactory().openSession();
	T entity = null;

	try {
		session.beginTransaction();
		entity = (T) session.get(getEntityClass(), pk);
		Hibernate.initialize(entity);
		session.getTransaction().commit();

	} catch (HibernateException hibernateException) {
		session.getTransaction().rollback();

		throw new SQLException(hibernateException);

	} finally {
		session.close();
	}

	return entity;
}
 
开发者ID:mrh3nry,项目名称:Celebino,代码行数:23,代码来源:GenericDao.java

示例5: updateBook

import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public int updateBook(long ISBN, int price) {
	// TODO Auto-generated method stub

	Session session = sessionFactory.openSession();
	Transaction transaction = session.beginTransaction();
	try {
		Book book = session.get(Book.class, ISBN);
		book.setPrice(price);

		session.saveOrUpdate(book);
		transaction.commit();
		session.close();
		return 1;
	} catch (DataAccessException exception) {
		exception.printStackTrace();
	}
	return 0;

}
 
开发者ID:PacktPublishing,项目名称:Learning-Spring-5.0,代码行数:21,代码来源:BookDAO_SessionFactory.java

示例6: show

import org.hibernate.Session; //导入方法依赖的package包/类
/**
 * Http requet which get a specific
 * user depending on the id.
 *
 * @param id user id
 * @return a response which contains the user or errors.
 */
@GET
@Path("{id}/")
public Response show(@PathParam("id") Long id) {
    LOGGER.info("#GET {" + id + "} ");
    Response response = null;
    if (this.authorization) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            User user = session.get(User.class, id);
            session.getTransaction().commit();
            session.close();

            if (user == null) {
                response = Error.notFound(String.valueOf(id))
                        .getResponse();
                LOGGER.warning("#GET {" + id + "} " + response.toString());
            } else {
                response = Response
                        .ok(user)
                        .build();
                LOGGER.info("#GET {" + id + "} " + response.toString());
            }

        } catch (Exception exception) {
            response = Error.internalServer(exception)
                    .getResponse();
            LOGGER.warning("#GET {" + id + "} " + exception.getLocalizedMessage());
            session.getTransaction().rollback();
        }
    } else {
        response = this.getUnauthorization();
    }
    return response;
}
 
开发者ID:YMonnier,项目名称:docker-restful-java,代码行数:43,代码来源:UserController.java

示例7: findById

import org.hibernate.Session; //导入方法依赖的package包/类
public Account findById(String identifier) {
	Session session = null;
	try {
		session = this.sessionFactory.openSession();
		return (Account) session.get(Account.class, identifier);
	} finally {
		this.closeSessionIfNecessary(session);
	}
}
 
开发者ID:liuyangming,项目名称:ByteJTA-sample,代码行数:10,代码来源:AccountDaoImpl.java

示例8: getNewsInfoById

import org.hibernate.Session; //导入方法依赖的package包/类
/**
 * 根据id取得记录
 */
@Override
public NewsInfo getNewsInfoById(int id) {
	Session session = SessionFactory.getCurrentSession();
	NewsInfo newsInfo = (NewsInfo) session.get(NewsInfo.class, id);
	return newsInfo;
}
 
开发者ID:RyougiChan,项目名称:NewsSystem,代码行数:10,代码来源:NewsInfoDAOImpl.java

示例9: addingNewChildToExistingParent

import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public void addingNewChildToExistingParent() {
	Session ses=null;
	Department dept=null;
	Employee emp=null;
	Transaction tx=null;
	//PhoneNumber ph3=null;
   //get Session
	ses=HibernateUtil.getSession();
	//Load parent object(Existing)
	dept=ses.get(Department.class,1001);
	//child objs
	 	emp=new Employee();
	 	emp.setEname("Amit");
	 	emp.setEsal(60000f);
	 	//set to existing dept
	 emp.setDept(dept);

	try{
		tx=ses.beginTransaction();
		 int idval=(Integer)ses.save(emp);
		tx.commit();
		System.out.println("new Employee is added to existing Department: "+idval);
	}//try
	catch(Exception e){
		tx.rollback();
	}
}
 
开发者ID:pratikdimble,项目名称:Hibernate_Association_Mapping_ManyToOne_Save_Load_Maven,代码行数:29,代码来源:MTO_DAOImpl.java

示例10: findByPrimary

import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public User findByPrimary(Long id) {
	User user = null;
	Session session = sessionFactory.getCurrentSession();
	try {
		session.beginTransaction();
		user = session.get(User.class, id);
		session.getTransaction().commit();
	} catch (RuntimeException e) {
		session.getTransaction().rollback();
	}
	return user;
}
 
开发者ID:lf23617358,项目名称:training-sample,代码行数:14,代码来源:HibernateOrderDao.java

示例11: testUnidirectionalManyToOne

import org.hibernate.Session; //导入方法依赖的package包/类
@Test
public void testUnidirectionalManyToOne() throws Exception {
	final Session session = openSession();
	Transaction transaction = session.beginTransaction();
	JUG jug = new JUG( "summer_camp" );
	jug.setName( "JUG Summer Camp" );
	session.persist( jug );
	Member emmanuel = new Member( "emmanuel" );
	emmanuel.setName( "Emmanuel Bernard" );
	emmanuel.setMemberOf( jug );
	Member jerome = new Member( "jerome" );
	jerome.setName( "Jerome" );
	jerome.setMemberOf( jug );
	session.persist( emmanuel );
	session.persist( jerome );
	session.flush();
	transaction.commit();
	assertThat( getNumberOfEntities( sessionFactory ) ).isEqualTo( 3 );
	assertThat( getNumberOfAssociations( sessionFactory ) ).isEqualTo( 0 );

	session.clear();

	transaction = session.beginTransaction();
	emmanuel = session.get( Member.class, emmanuel.getId() );
	jug = emmanuel.getMemberOf();
	session.delete( emmanuel );
	jerome = session.get( Member.class, jerome.getId() );
	session.delete( jerome );
	session.delete( jug );
	transaction.commit();
	assertThat( getNumberOfEntities( sessionFactory ) ).isEqualTo( 0 );
	assertThat( getNumberOfAssociations( sessionFactory ) ).isEqualTo( 0 );

	session.close();

	checkCleanCache();
}
 
开发者ID:hibernate,项目名称:hibernate-ogm-ignite,代码行数:38,代码来源:ManyToOneTest.java

示例12: getObjectById

import org.hibernate.Session; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public E getObjectById(PK id) {
			
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    
    session.beginTransaction();

    E entity = (E) session.get(entityClass, id);
    session.getTransaction().commit();
    return (E) entity;
}
 
开发者ID:mavogel,项目名称:hska-vis-legacy,代码行数:12,代码来源:GenericHibernateDAO.java

示例13: testNumberOfCallsToDatastore

import org.hibernate.Session; //导入方法依赖的package包/类
@Test
@BMRules(rules = {
		@BMRule(targetClass = "org.hibernate.ogm.datastore.redis.impl.json.JsonEntityStorageStrategy",
				targetMethod = "storeEntity(java.lang.String, org.hibernate.ogm.datastore.redis.dialect.value.Entity)",
				helper = "org.hibernate.ogm.utils.BytemanHelper",
				action = "countInvocation(\"storeEntity\")",
				name = "countStoreEntity"),
		@BMRule(targetClass = "org.hibernate.ogm.datastore.redis.impl.json.JsonEntityStorageStrategy",
				targetMethod = "getEntity(java.lang.String)",
				helper = "org.hibernate.ogm.utils.BytemanHelper",
				action = "countInvocation(\"getEntity\")",
				name = "countGetEntity")
})
public void testNumberOfCallsToDatastore() throws Exception {
	//insert entity with embedded collection
	Session session = openSession();
	Transaction tx = session.beginTransaction();
	GrandChild luke = new GrandChild();
	luke.setName( "Luke" );
	GrandChild leia = new GrandChild();
	leia.setName( "Leia" );
	GrandMother grandMother = new GrandMother();
	grandMother.getGrandChildren().add( luke );
	grandMother.getGrandChildren().add( leia );
	session.persist( grandMother );
	tx.commit();

	session.clear();

	int getEntityInvocationCount = BytemanHelper.getAndResetInvocationCount( "getEntity" );
	int storeEntityInvocationCount = BytemanHelper.getAndResetInvocationCount( "storeEntity" );
	assertThat( getEntityInvocationCount ).isEqualTo( 1 );
	assertThat( storeEntityInvocationCount ).isEqualTo( 1 );

	// Check that all the counters have been reset to 0
	getEntityInvocationCount = BytemanHelper.getAndResetInvocationCount( "getEntity" );
	storeEntityInvocationCount = BytemanHelper.getAndResetInvocationCount( "storeEntity" );
	assertThat( getEntityInvocationCount ).isEqualTo( 0 );
	assertThat( storeEntityInvocationCount ).isEqualTo( 0 );

	// Remove one of the elements
	tx = session.beginTransaction();
	grandMother = (GrandMother) session.get( GrandMother.class, grandMother.getId() );
	grandMother.getGrandChildren().remove( 0 );
	tx.commit();
	session.clear();

	getEntityInvocationCount = BytemanHelper.getAndResetInvocationCount( "getEntity" );
	storeEntityInvocationCount = BytemanHelper.getAndResetInvocationCount( "storeEntity" );
	assertThat( getEntityInvocationCount ).isEqualTo( 1 );
	assertThat( storeEntityInvocationCount ).isEqualTo( 1 );

	// Assert removal has been propagated
	tx = session.beginTransaction();
	grandMother = (GrandMother) session.get( GrandMother.class, grandMother.getId() );
	assertThat( grandMother.getGrandChildren() ).onProperty( "name" ).containsExactly( "Leia" );

	session.delete( grandMother );
	tx.commit();

	session.close();
}
 
开发者ID:hibernate,项目名称:hibernate-ogm-redis,代码行数:63,代码来源:RedisPerformanceTest.java

示例14: testDefaultBiDirManyToOneCompositeKeyTest

import org.hibernate.Session; //导入方法依赖的package包/类
@Test
public void testDefaultBiDirManyToOneCompositeKeyTest() throws Exception {

	//given
	Session session = openSession();
	Transaction transaction = session.beginTransaction();
	Court court = new Court();
	court.setId( new Court.CourtId() );
	court.getId().setCountryCode( "DE" );
	court.getId().setSequenceNo( 123 );
	court.setName( "Hamburg Court" );
	session.persist( court );
	Game game1 = new Game();
	game1.setId( new Game.GameId() );
	game1.getId().setCategory( "primary" );
	game1.getId().setSequenceNo( 456 );
	game1.setName( "The game" );
	game1.setPlayedOn( court );
	court.getGames().add( game1 );
	Game game2 = new Game();
	game2.setId( new Game.GameId() );
	game2.getId().setCategory( "primary" );
	game2.getId().setSequenceNo( 457 );
	game2.setName( "The other game" );
	game2.setPlayedOn( court );
	session.persist( game1 );
	session.persist( game2 );
	session.flush();
	transaction.commit();


	// when
	String representation = new String(
			getProvider().getConnection().get(
					"Court:{\"id.countryCode\":\"DE\",\"id.sequenceNo\":123}"
			)
	);

	// then
	JSONAssert.assertEquals(
			"{\"games\":[{\"gameSequenceNo\":456,\"category\":\"primary\"}," +
					"{\"gameSequenceNo\":457,\"category\":\"primary\"}]," +
					"\"name\":\"Hamburg Court\"}",
			representation,
			JSONCompareMode.NON_EXTENSIBLE
	);


	session.clear();

	transaction = session.beginTransaction();
	Court localCourt = session.get( Court.class, new Court.CourtId( "DE", 123 ) );
	assertThat( localCourt.getGames() ).hasSize( 2 );
	for ( Game game : localCourt.getGames() ) {
		session.delete( game );
	}
	localCourt.getGames().clear();
	session.delete( localCourt );
	transaction.commit();

	session.close();
}
 
开发者ID:hibernate,项目名称:hibernate-ogm-redis,代码行数:63,代码来源:ManyToOneInEntityJsonRepresentationTest.java

示例15: initValue

import org.hibernate.Session; //导入方法依赖的package包/类
public void initValue(Context context){
	Session session=context.getSession();
	blob=(Blob)session.get(Blob.class,blobId);
	text=(String)SerializationUtils.deserialize(blob.getBlobValue());
}
 
开发者ID:youseries,项目名称:uflo,代码行数:6,代码来源:TextVariable.java


注:本文中的org.hibernate.Session.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。