Java Code Examples for org.hibernate.Session.persist()

The following are Jave code examples for showing how to use persist() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: hibernate-ogm-ignite   File: ManyToOneTest.java   View Source Code Vote up 5 votes
@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();
}
 
Example 2
Project: SandBoxNGSB   File: QuoteDAOImpl.java   View Source Code Vote up 5 votes
@Override
public void save(QuotePE pe) {
   	Session session = getSession();
	Transaction tx = session.beginTransaction();
	session.persist(pe);
	tx.commit();
	session.close();
}
 
Example 3
Project: Library   File: LibraryServicesImpl.java   View Source Code Vote up 5 votes
/**
 * 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;

}
 
Example 4
Project: hibernate-ogm-redis   File: RedisPerformanceTest.java   View Source Code Vote up 4 votes
@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();
}
 
Example 5
Project: hibernate-ogm-redis   File: ManyToOneInEntityJsonRepresentationTest.java   View Source Code Vote up 4 votes
@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();
}
 
Example 6
Project: Homework   File: PetDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
Example 7
Project: Homework   File: PetDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
Example 8
Project: Homework   File: PetDaoImpl.java   View Source Code Vote up 4 votes
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
Example 9
Project: Homework   File: PetDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
Example 10
Project: Homework   File: PetDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
Example 11
Project: Homework   File: PetDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
Example 12
Project: Homework   File: PetDaoImpl.java   View Source Code Vote up 4 votes
@Override
public void save(Pet pet, Session session, Transaction tx)
{
    session.persist(pet);
    session.flush();
}
 
Example 13
Project: hibernate-ogm-ignite   File: ManyToOneTest.java   View Source Code Vote up 4 votes
@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();
}
 
Example 14
Project: hibernate-ogm-ignite   File: ManyToOneTest.java   View Source Code Vote up 4 votes
@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();
}
 
Example 15
Project: Restaurant   File: OrderDaoImpl.java   View Source Code Vote up 4 votes
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());
		
	}