|
Hibernate example source code file (BasicHibernateAnnotationsTest.java)
The Hibernate BasicHibernateAnnotationsTest.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat, Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.test.annotations.entity; import java.math.BigDecimal; import java.util.Currency; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.hibernate.AnnotationException; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.ServiceRegistryBuilder; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** * @author Emmanuel Bernard */ public class BasicHibernateAnnotationsTest extends BaseCoreFunctionalTestCase { @Test @RequiresDialectFeature( DialectChecks.SupportsExpectedLobUsagePattern.class ) public void testEntity() throws Exception { Forest forest = new Forest(); forest.setName( "Fontainebleau" ); Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.persist( forest ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); forest = (Forest) s.get( Forest.class, forest.getId() ); assertNotNull( forest ); forest.setName( "Fontainebleau" ); //should not execute SQL update tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); forest = (Forest) s.get( Forest.class, forest.getId() ); assertNotNull( forest ); forest.setLength( 23 ); //should execute dynamic SQL update tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); s.delete( s.get( Forest.class, forest.getId() ) ); tx.commit(); s.close(); } @Test @RequiresDialectFeature( DialectChecks.SupportsExpectedLobUsagePattern.class ) public void testVersioning() throws Exception { Forest forest = new Forest(); forest.setName( "Fontainebleau" ); forest.setLength( 33 ); Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.persist( forest ); tx.commit(); s.close(); Session parallelSession = openSession(); Transaction parallelTx = parallelSession.beginTransaction(); s = openSession(); tx = s.beginTransaction(); forest = (Forest) parallelSession.get( Forest.class, forest.getId() ); Forest reloadedForest = (Forest) s.get( Forest.class, forest.getId() ); reloadedForest.setLength( 11 ); assertNotSame( forest, reloadedForest ); tx.commit(); s.close(); forest.setLength( 22 ); try { parallelTx.commit(); fail( "All optimistic locking should have make it fail" ); } catch (HibernateException e) { if ( parallelTx != null ) parallelTx.rollback(); } finally { parallelSession.close(); } s = openSession(); tx = s.beginTransaction(); s.delete( s.get( Forest.class, forest.getId() ) ); tx.commit(); s.close(); } @Test @RequiresDialectFeature( DialectChecks.SupportsExpectedLobUsagePattern.class ) public void testPolymorphism() throws Exception { Forest forest = new Forest(); forest.setName( "Fontainebleau" ); forest.setLength( 33 ); Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.persist( forest ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); Query query = s.createQuery( "from java.lang.Object" ); assertEquals( 0, query.list().size() ); query = s.createQuery( "from Forest" ); assertTrue( 0 < query.list().size() ); tx.commit(); s.close(); } @Test @RequiresDialectFeature( DialectChecks.SupportsExpectedLobUsagePattern.class ) public void testType() throws Exception { Forest f = new Forest(); f.setName( "Broceliande" ); String description = "C'est une enorme foret enchantee ou vivais Merlin et toute la clique"; f.setLongDescription( description ); Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.persist( f ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); f = (Forest) s.get( Forest.class, f.getId() ); assertNotNull( f ); assertEquals( description, f.getLongDescription() ); s.delete( f ); tx.commit(); s.close(); } /* * Test import of TypeDefs from MappedSuperclass and * Embedded classes. * The classes 'Name' and 'FormalLastName' both embed the same * component 'LastName'. This is to verify that processing the * typedef defined in the component TWICE does not create any * issues. */ @Test public void testImportTypeDefinitions() throws Exception { LastName lastName = new LastName(); lastName.setName("reddy"); Name name = new Name(); name.setFirstName("SHARATH"); name.setLastName(lastName); FormalLastName formalName = new FormalLastName(); formalName.setLastName(lastName); formalName.setDesignation("Mr"); Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.persist(name); s.persist(formalName); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); name = (Name) s.get( Name.class, name.getId() ); assertNotNull( name ); assertEquals( "sharath", name.getFirstName() ); assertEquals( "REDDY", name.getLastName().getName() ); formalName = (FormalLastName) s.get(FormalLastName.class, formalName.getId()); assertEquals( "REDDY", formalName.getLastName().getName() ); s.delete(name); s.delete(formalName); tx.commit(); s.close(); } @Test public void testNonLazy() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); Forest f = new Forest(); Tree t = new Tree(); t.setName( "Basic one" ); s.persist( f ); s.persist( t ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); f = (Forest) s.load( Forest.class, f.getId() ); t = (Tree) s.load( Tree.class, t.getId() ); assertFalse( "Default should be lazy", Hibernate.isInitialized( f ) ); assertTrue( "Tree is not lazy", Hibernate.isInitialized( t ) ); tx.commit(); s.close(); } @Test public void testCache() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); ZipCode zc = new ZipCode(); zc.code = "92400"; s.persist( zc ); tx.commit(); s.close(); sessionFactory().getStatistics().clear(); sessionFactory().getStatistics().setStatisticsEnabled( true ); sessionFactory().evict( ZipCode.class ); s = openSession(); tx = s.beginTransaction(); s.get( ZipCode.class, zc.code ); assertEquals( 1, sessionFactory().getStatistics().getSecondLevelCachePutCount() ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); s.get( ZipCode.class, zc.code ); assertEquals( 1, sessionFactory().getStatistics().getSecondLevelCacheHitCount() ); tx.commit(); s.close(); } @Test public void testFilterOnCollection() { Session s = openSession(); Transaction tx = s.beginTransaction(); Topic topic = new Topic(); Narrative n1 = new Narrative(); n1.setState("published"); topic.addNarrative(n1); Narrative n2 = new Narrative(); n2.setState("draft"); topic.addNarrative(n2); s.persist(topic); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); topic = (Topic) s.load( Topic.class, topic.getId() ); s.enableFilter("byState").setParameter("state", "published"); topic = (Topic) s.load( Topic.class, topic.getId() ); assertNotNull(topic); assertTrue(topic.getNarratives().size() == 1); assertEquals("published", topic.getNarratives().iterator().next().getState()); tx.commit(); s.close(); } @Test public void testCascadedDeleteOfChildEntitiesBug2() { // Relationship is one SoccerTeam to many Players. // Create a SoccerTeam (parent) and three Players (child). // Verify that the count of Players is correct. // Clear the SoccerTeam reference Players. // The orphanRemoval should remove the Players automatically. // @OneToMany(mappedBy="name", orphanRemoval=true) Session s = openSession(); Transaction tx = s.beginTransaction(); SoccerTeam team = new SoccerTeam(); int teamid = team.getId(); Player player1 = new Player(); player1.setName("Shalrie Joseph"); team.addPlayer(player1); Player player2 = new Player(); player2.setName("Taylor Twellman"); team.addPlayer(player2); Player player3 = new Player(); player3.setName("Steve Ralston"); team.addPlayer(player3); s.persist(team); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); team = (SoccerTeam)s.merge(team); int count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue(); assertEquals("expected count of 3 but got = " + count, count, 3); // clear references to players, this should orphan the players which should // in turn trigger orphanRemoval logic. team.getPlayers().clear(); // count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue(); // assertEquals("expected count of 0 but got = " + count, count, 0); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue(); assertEquals("expected count of 0 but got = " + count, count, 0); tx.commit(); s.close(); } @Test public void testCascadedDeleteOfChildOneToOne() { // create two single player teams (for one versus one match of soccer) // and associate teams with players via the special OneVOne methods. // Clear the Team reference to players, which should orphan the teams. // Orphaning the team should delete the team. Session s = openSession(); Transaction tx = s.beginTransaction(); SoccerTeam team = new SoccerTeam(); team.setName("Shalrie's team"); Player player1 = new Player(); player1.setName("Shalrie Joseph"); team.setOneVonePlayer(player1); player1.setOneVoneTeam(team); s.persist(team); SoccerTeam team2 = new SoccerTeam(); team2.setName("Taylor's team"); Player player2 = new Player(); player2.setName("Taylor Twellman"); team2.setOneVonePlayer(player2); player2.setOneVoneTeam(team2); s.persist(team2); tx.commit(); tx = s.beginTransaction(); s.clear(); team2 = (SoccerTeam)s.load(team2.getClass(), team2.getId()); team = (SoccerTeam)s.load(team.getClass(), team.getId()); int count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue(); assertEquals("expected count of 2 but got = " + count, count, 2); // clear references to players, this should orphan the players which should // in turn trigger orphanRemoval logic. team.setOneVonePlayer(null); team2.setOneVonePlayer(null); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); count = ( (Long) s.createQuery( "select count(*) from Player" ).iterate().next() ).intValue(); assertEquals("expected count of 0 but got = " + count, count, 0); tx.commit(); s.close(); } @Test public void testFilter() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.createQuery( "delete Forest" ).executeUpdate(); Forest f1 = new Forest(); f1.setLength( 2 ); s.persist( f1 ); Forest f2 = new Forest(); f2.setLength( 20 ); s.persist( f2 ); Forest f3 = new Forest(); f3.setLength( 200 ); s.persist( f3 ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); s.enableFilter( "betweenLength" ).setParameter( "minLength", 5 ).setParameter( "maxLength", 50 ); long count = ( (Long) s.createQuery( "select count(*) from Forest" ).iterate().next() ).intValue(); assertEquals( 1, count ); s.disableFilter( "betweenLength" ); s.enableFilter( "minLength" ).setParameter( "minLength", 5 ); count = ( (Long) s.createQuery( "select count(*) from Forest" ).iterate().next() ).longValue(); assertEquals( 2l, count ); s.disableFilter( "minLength" ); tx.rollback(); s.close(); } /** * Tests the functionality of inheriting @Filter and @FilterDef annotations * defined on a parent MappedSuperclass(s) */ @Test public void testInheritFiltersFromMappedSuperclass() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); s.createQuery( "delete Drill" ).executeUpdate(); Drill d1 = new PowerDrill(); d1.setName("HomeDrill1"); d1.setCategory("HomeImprovment"); s.persist( d1 ); Drill d2 = new PowerDrill(); d2.setName("HomeDrill2"); d2.setCategory("HomeImprovement"); s.persist(d2); Drill d3 = new PowerDrill(); d3.setName("HighPowerDrill"); d3.setCategory("Industrial"); s.persist( d3 ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); //We test every filter with 2 queries, the first on the base class of the //inheritance hierarchy (Drill), and the second on a subclass (PowerDrill) s.enableFilter( "byName" ).setParameter( "name", "HomeDrill1"); long count = ( (Long) s.createQuery( "select count(*) from Drill" ).iterate().next() ).intValue(); assertEquals( 1, count ); count = ( (Long) s.createQuery( "select count(*) from PowerDrill" ).iterate().next() ).intValue(); assertEquals( 1, count ); s.disableFilter( "byName" ); s.enableFilter( "byCategory" ).setParameter( "category", "Industrial" ); count = ( (Long) s.createQuery( "select count(*) from Drill" ).iterate().next() ).longValue(); assertEquals( 1, count ); count = ( (Long) s.createQuery( "select count(*) from PowerDrill" ).iterate().next() ).longValue(); assertEquals( 1, count ); s.disableFilter( "byCategory" ); tx.rollback(); s.close(); } @Test @RequiresDialectFeature( DialectChecks.SupportsExpectedLobUsagePattern.class ) public void testParameterizedType() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); Forest f = new Forest(); f.setSmallText( "ThisIsASmallText" ); f.setBigText( "ThisIsABigText" ); s.persist( f ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); Forest f2 = (Forest) s.get( Forest.class, f.getId() ); assertEquals( f.getSmallText().toLowerCase(), f2.getSmallText() ); assertEquals( f.getBigText().toUpperCase(), f2.getBigText() ); tx.commit(); s.close(); } @Test @RequiresDialectFeature( DialectChecks.SupportsExpectedLobUsagePattern.class ) public void testSerialized() throws Exception { Forest forest = new Forest(); forest.setName( "Shire" ); Country country = new Country(); country.setName( "Middle Earth" ); forest.setCountry( country ); Set<Country> near = new HashSet Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate BasicHibernateAnnotationsTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.