Session get - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Session get method.

This code example shows how to use the following methods:close, delete, get, beginTransaction
				c.setName( "Palais des festivals" );
				tx.commit();
				s.close();
 
				s = sf.openSession();
				tx = s.beginTransaction();
				w = (Worker) s.get( Worker.class, w.getId() );
				s.delete( w );
				c = (Construction) s.get( Construction.class, c.getId() );
				s.delete( c );
				tx.commit();
				s.close();
			} 
			catch (Throwable t) {
				t.printStackTrace();
			} 
		} 
	} 
 
	@Override 
7
CodeRank
This code example shows how to use the following methods:close, doWork, get, getTransaction
	 * 
	 * @throws SQLException in case the insert fails. 
	 */ 
	private TShirt createObjectWithSQL() throws SQLException {
		Session s = openSession();
		s.getTransaction().begin();
		s.doWork( new Work() {
			@Override 
			public void execute(Connection connection) throws SQLException {
				final Statement statement = connection.createStatement();
				statement.executeUpdate(
						"insert into TShirt_Master(id, logo, size_, length_) values( 1, 'JBoss balls', 'large', 23.2)" 
				); 
				statement.close();
			} 
		} ); 
		TShirt ts = (TShirt) s.get( TShirt.class, 1 );
		s.getTransaction().commit();
		s.close();
		return ts;

7
CodeRank
This code example shows how to use the following methods:clear, close, delete, get, persist
		Puppy kittenOne = new Puppy( "kitten one" );
		Puppy KittenTwo = new Puppy( "kitten two" );
		cat.addPuppy( kittenOne ).addPuppy( KittenTwo );
 
		Session s = openSession();
		Transaction tx = s.beginTransaction();
		s.persist( withoutPuppies );
		s.persist( wihKittens );
		s.persist( dog );
		s.persist( cat );
		s.persist( kittenOne );
		s.persist( KittenTwo );
		tx.commit();
 
		try { 
			findNullsFor( s, "partner", "indexAsNull not set" );
			fail( "Embedded null field should not exists for field without indexAsNull property" ); 
		} 
		catch (SearchException e) {
			// Succeded: indexAsNull not specified so the field is not created 

7
CodeRank
This code example shows how to use the following methods:get
		Exception e, Session session, Object object) {
 
		if (e instanceof org.hibernate.StaleObjectStateException) {
			BaseModel<?> baseModel = (BaseModel<?>)object;
 
			Object currentObject = session.get(
				object.getClass(), baseModel.getPrimaryKeyObj());
 
			return new ORMException( 
				object + " is stale in comparison to " + currentObject, e);
		} 
		else { 
			return new ORMException(e);
		} 
	} 
 
} 
6
CodeRank
This code example shows how to use the following methods:clear, close, flush, get, persist
 */ 
public class QuoteTest extends BaseCoreFunctionalTestCase { 
	@Test
	public void testQuoteManytoMany() { 
		Session s = openSession();
		Transaction tx = s.beginTransaction();
		User u = new User();
		s.persist( u );
		Role r = new Role();
		s.persist( r );
		u.getRoles().add( r );
		s.flush();
		s.clear();
		u = (User) s.get( User.class, u.getId() );
		assertEquals( 1, u.getRoles().size() );
		tx.rollback();
		String role = User.class.getName() + ".roles";
		assertEquals( "User_Role", configuration().getCollectionMapping( role ).getCollectionTable().getName() );
		s.close();
	} 

6
CodeRank
This code example shows how to use the following methods:close, get
		Session session = openSession();
 
		try { 
			if ( loadAbstractProxyBeforeSearch ) {
				// Load a proxified version of the entity into the session 
				LazyAbstractEntityReference reference = (LazyAbstractEntityReference) session.get(
						LazyAbstractEntityReference.class, entityReferenceId );
				assertTrue( reference != null && !Hibernate.isInitialized( reference.getEntity() ) );
			} 
 
			// Search for the created entity 
			assertEquals( 1, doSearch( session, clazz, entityId ).size() );
		} 
		finally { 
			session.close();
		} 
	} 
 
	private void populateDatabase() { 
		Session session = openSession();
7
CodeRank
This code example shows how to use the following methods:close, get, beginTransaction, createQuery, getTransaction
		emp.setInfo( null );
		session.getTransaction().commit();
		session.close();
 
		session = openSession();
		session.beginTransaction();
		emp = ( Employee ) session.get( Employee.class, emp.getId() );
		assertNull( emp.getInfo() );
		results = session.createQuery( "from EmployeeInfo" ).list();
		assertEquals( 0, results.size() );
		results = session.createQuery( "from Employee" ).list();
		assertEquals( 1, results.size() );
		session.getTransaction().commit();
		session.close();
 
		cleanupData(); 
	} 
} 
6
CodeRank
This code example shows how to use the following methods:close, flush, get, save, getTransaction
		Sale sale = new Sale();
		Contact contact = new Contact();
		contact.setName( "Emmanuel" );
		sale.getContacts().add(contact);
		Session s = openSession(  );
		s.getTransaction().begin();
		s.save( sale );
		s.flush();
		s.get( Sale.class, sale.getId() );
		assertEquals( 1, sale.getContacts().size() );
		s.getTransaction().rollback();
		s.close();
	} 
 
	@Override 
	protected Class[] getAnnotatedClasses() {
		return new Class[] {
				Sale.class 
		}; 
	} 
6
CodeRank
This code example shows how to use the following methods:clear, close, flush, get, persist
 */ 
public class QuoteGlobalTest extends BaseCoreFunctionalTestCase { 
	@Test
	public void testQuoteManytoMany() { 
		Session s = openSession();
		Transaction tx = s.beginTransaction();
		User u = new User();
		s.persist( u );
		Role r = new Role();
		s.persist( r );
		u.getRoles().add( r );
		s.flush();
		s.clear();
		u = (User) s.get( User.class, u.getId() );
		assertEquals( 1, u.getRoles().size() );
		tx.rollback();
		String role = User.class.getName() + ".roles";
		assertEquals( "User_Role", configuration().getCollectionMapping( role ).getCollectionTable().getName() );
		s.close();
	} 

6
CodeRank
This code example shows how to use the following methods:close, get, beginTransaction
		c.setAge(18);
		t.commit();
		s.close();
 
		s = openSession();
		t = s.beginTransaction();
		p = (Parent) s.get(Parent.class, "Marc");
		p2 = (Parent) s.get(Parent.class, "Nathalie");
		c = (Child) s.get(Child.class, "Elvira");
		assertEquals( p.getChildren().indexOf(c), 0 );
		p.getChildren().remove(c);
		p2.getChildren().add(c);
		t.commit();
 
		s.close();
		s = openSession();
		t = s.beginTransaction();
		Parent p3 = new Parent("Marion");
		p3.getChildren().add( new Child("Gavin") );
		s.merge(p3);
6
CodeRank
get Method Overview
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
See Code Examples for other Hibernate ORM Session Methods: