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


Java Session.persist方法代码示例

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


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

示例1: 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

示例2: save

import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public void save(QuotePE pe) {
   	Session session = getSession();
	Transaction tx = session.beginTransaction();
	session.persist(pe);
	tx.commit();
	session.close();
}
 
开发者ID:auTasso,项目名称:SandBoxNGSB,代码行数:9,代码来源:QuoteDAOImpl.java

示例3: addBorrower

import org.hibernate.Session; //导入方法依赖的package包/类
/**
 * Add Borrower
 */
public RestResponse addBorrower(Borrower borrower) {

	RestResponse response = new RestResponse();
	Session session = this.sessionFactory.openSession();
	Transaction transaction = (Transaction) session.beginTransaction();

	String queryString = "from Borrower where ssn=:ssn";
	Query query = session.createQuery(queryString);
	query.setString("ssn", borrower.getSsn());
	Object object = query.uniqueResult();

	if (object != null) {
		response.setError("Account already exists");
		response.setSuccess(false);

	} else {

		session.persist(borrower);
		transaction.commit();

		query = session.createQuery(queryString);
		query.setString("ssn", borrower.getSsn());
		object = query.uniqueResult();

		Borrower borrower2 = (Borrower) object;
		response.setSuccess(true);
		response.setResult(String.valueOf(borrower2.getCardId()));
	}

	session.close();

	return response;

}
 
开发者ID:axd164330,项目名称:Library,代码行数:38,代码来源:LibraryServicesImpl.java

示例4: 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

示例5: 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

示例6: save

import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
开发者ID:JAVA201708,项目名称:Homework,代码行数:7,代码来源:PetDaoImpl.java

示例7: save

import org.hibernate.Session; //导入方法依赖的package包/类
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
开发者ID:JAVA201708,项目名称:Homework,代码行数:6,代码来源:PetDaoImpl.java

示例8: testBiDirManyToOneInsertUpdateFalse

import org.hibernate.Session; //导入方法依赖的package包/类
@Test
public void testBiDirManyToOneInsertUpdateFalse() throws Exception {
	final Session session = openSession();
	Transaction tx = session.beginTransaction();
	Beer hoegaarden = new Beer();
	Brewery hoeBrewery = new Brewery();
	hoeBrewery.getBeers().add( hoegaarden );
	hoegaarden.setBrewery( hoeBrewery );
	session.persist( hoeBrewery );
	tx.commit();
	session.clear();

	tx = session.beginTransaction();
	hoegaarden = get( session, Beer.class, hoegaarden.getId() );
	assertThat( hoegaarden ).isNotNull();
	assertThat( hoegaarden.getBrewery() ).isNotNull();
	assertThat( hoegaarden.getBrewery().getBeers() )
		.hasSize( 1 )
		.containsOnly( hoegaarden );
	Beer citron = new Beer();
	hoeBrewery = hoegaarden.getBrewery();
	hoeBrewery.getBeers().remove( hoegaarden );
	hoeBrewery.getBeers().add( citron );
	citron.setBrewery( hoeBrewery );
	session.delete( hoegaarden );
	tx.commit();

	session.clear();

	tx = session.beginTransaction();
	citron = get( session, Beer.class, citron.getId() );
	assertThat( citron.getBrewery().getBeers() )
		.hasSize( 1 )
		.containsOnly( citron );
	hoeBrewery = citron.getBrewery();
	citron.setBrewery( null );
	hoeBrewery.getBeers().clear();
	session.delete( citron );
	session.delete( hoeBrewery );
	tx.commit();

	session.close();

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

示例9: testDefaultBiDirManyToOneCompositeKeyTest

import org.hibernate.Session; //导入方法依赖的package包/类
@Test
public void testDefaultBiDirManyToOneCompositeKeyTest() throws Exception {
	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();

	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-ignite,代码行数:43,代码来源:ManyToOneTest.java

示例10: saveOrder

import org.hibernate.Session; //导入方法依赖的package包/类
public void saveOrder(ShoppingCart cart) {	

		Session session = this.sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		
		User user = null;

		if (this.getUser(cart.getCustomerInfo().getEmail()) == null) {
			user = new User();
			user.setfName(cart.getCustomerInfo().getfName());
			user.setlName(cart.getCustomerInfo().getlName());
			user.setEmailId(cart.getCustomerInfo().getEmail());
			user.setPhone(cart.getCustomerInfo().getPhone());
			user.setAddress(cart.getCustomerInfo().getAddress());
			session.persist(user);			
		}else{
			user = this.getUser(cart.getCustomerInfo().getEmail());
		}
		
				
				
		Order order = new Order(user);
		order.setOrderId(this.getMaxOrderNum()+1);
		order.setPrice(cart.getTotalPrice());
		order.setDate(new Timestamp(System.currentTimeMillis()));
		
		session.persist(order);
		
		for (ItemInfo info : cart.getCartItem()) {
			OrderItems items = new OrderItems(order);
			items.setQuantity(info.getQuantity());
			items.setName(info.getProductInfo().getProductName());
			items.setHotnessLevel(info.getHotnessLevel());
			session.persist(items);
		}
		
		
		transaction.commit();
		session.close();
		
		cart.setOrderNumber(order.getOrderId());
		
	}
 
开发者ID:axd164330,项目名称:Restaurant,代码行数:44,代码来源:OrderDaoImpl.java


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