[hibernate-commits] Hibernate SVN: r11231 - in branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations: loader and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Feb 22 14:58:31 EST 2007


Author: epbernard
Date: 2007-02-22 14:58:31 -0500 (Thu, 22 Feb 2007)
New Revision: 11231

Added:
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Loader.hbm.xml
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/LoaderTest.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Player.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Team.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/TestCase.java
Log:
Test on colelction loaders

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Loader.hbm.xml
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Loader.hbm.xml	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Loader.hbm.xml	2007-02-22 19:58:31 UTC (rev 11231)
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<!--
+
+  Used to demonstrate the declarative configuration
+  of both hbm files and annotated classes
+  See hibernate.cfg.xml and ConfigurationTest
+
+-->
+
+<hibernate-mapping package="org.hibernate.test.annotations.loader">
+
+    <sql-query name="loadByTeam">
+		<load-collection alias="p" role="Team.players"/>
+		select {p.*} from Player p where p.team_id =  ?
+	</sql-query>
+
+</hibernate-mapping>
\ No newline at end of file

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/LoaderTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/LoaderTest.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/LoaderTest.java	2007-02-22 19:58:31 UTC (rev 11231)
@@ -0,0 +1,69 @@
+//$Id: EntityTest.java 9838 2006-04-30 19:00:59Z epbernard $
+package org.hibernate.test.annotations.loader;
+
+import java.util.Set;
+import java.util.Iterator;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.test.annotations.TestCase;
+
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class LoaderTest extends TestCase {
+
+
+	protected String[] getXmlFiles() {
+		return new String[] {
+				"org/hibernate/test/annotations/loader/Loader.hbm.xml"
+		};
+	}
+
+	public void testBasic() throws Exception {
+		Session s = openSession( );
+		Transaction tx = s.beginTransaction();
+		Team t = new Team();
+		Player p = new Player();
+		p.setName("me");
+		t.getPlayers().add(p);
+		p.setTeam(t);
+		
+
+		try {
+			s.persist(p);
+			s.persist(t);
+			tx.commit();
+			s.close();
+			
+			s= openSession( );
+			tx = s.beginTransaction();
+			Team t2 = (Team)s.load(Team.class,new Long(1));
+			Set<Player> players = t2.getPlayers();
+			Iterator<Player> iterator = players.iterator();
+			assertEquals("me", iterator.next().getName());
+			tx.commit();
+			
+		}
+		catch (Exception e) {
+			e.printStackTrace();
+			if ( tx != null ) tx.rollback();
+		}
+		finally {
+			s.close();
+		}
+	}
+
+	/**
+	 * @see org.hibernate.test.annotations.TestCase#getMappings()
+	 */
+	protected Class[] getMappings() {
+		return new Class[]{
+				Player.class,
+				Team.class
+		};
+	}
+
+}
+


Property changes on: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/LoaderTest.java
___________________________________________________________________
Name: svn:executable
   + *

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Player.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Player.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Player.java	2007-02-22 19:58:31 UTC (rev 11231)
@@ -0,0 +1,48 @@
+package org.hibernate.test.annotations.loader;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+
+import org.hibernate.annotations.Fetch;
+import org.hibernate.annotations.FetchMode;
+
+ at Entity
+public class Player {
+	
+	private Long id;
+	private Team team;
+	private String name;
+	
+    @Id
+    @GeneratedValue
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	@ManyToOne(targetEntity = Team.class)
+	@Fetch(FetchMode.SELECT)
+	@JoinColumn(name = "team_id")
+	public Team getTeam() {
+		return team;
+	}
+
+	public void setTeam(Team team) {
+		this.team = team;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+	
+}


Property changes on: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Player.java
___________________________________________________________________
Name: svn:executable
   + *

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Team.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Team.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Team.java	2007-02-22 19:58:31 UTC (rev 11231)
@@ -0,0 +1,45 @@
+package org.hibernate.test.annotations.loader;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+import org.hibernate.annotations.Fetch;
+import org.hibernate.annotations.FetchMode;
+import org.hibernate.annotations.Loader;
+import org.hibernate.annotations.NamedNativeQueries;
+import org.hibernate.annotations.NamedNativeQuery;
+
+import javax.persistence.NamedQuery;
+
+ at Entity
+public class Team {
+	private Long id;
+	private Set<Player> players = new HashSet<Player>();
+	
+    @Id
+    @GeneratedValue
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+	
+	@OneToMany(targetEntity = Player.class, mappedBy = "team", fetch = FetchType.EAGER)
+	@Fetch(FetchMode.SELECT)
+	@Loader(namedQuery = "loadByTeam")
+	public Set<Player> getPlayers() {
+		return players;
+	}
+
+	public void setPlayers(Set<Player> players) {
+		this.players = players;
+	}
+}


Property changes on: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/Team.java
___________________________________________________________________
Name: svn:executable
   + *

Added: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/TestCase.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/TestCase.java	                        (rev 0)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/loader/TestCase.java	2007-02-22 19:58:31 UTC (rev 11231)
@@ -0,0 +1,146 @@
+package org.hibernate.test.annotations.loader;
+import java.io.InputStream;
+
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.AnnotationConfiguration;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.dialect.Dialect;
+
+public abstract class TestCase extends junit.framework.TestCase {
+
+	private static SessionFactory sessions;
+	private static AnnotationConfiguration cfg;
+	private static Dialect dialect;
+	private static Class lastTestClass;
+	private Session session;
+
+	public TestCase() {
+		super();
+	}
+
+	public TestCase(String x) {
+		super( x );
+	}
+
+	protected void buildSessionFactory(Class[] classes, String[] packages, String[] xmlFiles) throws Exception {
+
+		if ( getSessions() != null ) getSessions().close();
+		try {
+			setCfg( new AnnotationConfiguration() );
+			configure( cfg );
+			if ( recreateSchema() ) {
+				cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
+			}
+			for ( int i = 0; i < packages.length ; i++ ) {
+				getCfg().addPackage( packages[i] );
+			}
+			for ( int i = 0; i < classes.length ; i++ ) {
+				getCfg().addAnnotatedClass( classes[i] );
+			}
+			for ( int i = 0; i < xmlFiles.length ; i++ ) {
+				InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFiles[i] );
+				getCfg().addInputStream( is );
+			}
+			setDialect( Dialect.getDialect() );
+			setSessions( getCfg().buildSessionFactory( /*new TestInterceptor()*/ ) );
+		}
+		catch (Exception e) {
+			e.printStackTrace();
+			throw e;
+		}
+	}
+
+	protected void setUp() throws Exception {
+		if ( getSessions() == null || lastTestClass != getClass() ) {
+			buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
+			lastTestClass = getClass();
+		}
+	}
+
+	protected void runTest() throws Throwable {
+		try {
+			super.runTest();
+			if ( session != null && session.isOpen() ) {
+				if ( session.isConnected() ) session.connection().rollback();
+				session.close();
+				session = null;
+				fail( "unclosed session" );
+			}
+			else {
+				session = null;
+			}
+		}
+		catch (Throwable e) {
+			try {
+				if ( session != null && session.isOpen() ) {
+					if ( session.isConnected() ) session.connection().rollback();
+					session.close();
+				}
+			}
+			catch (Exception ignore) {
+			}
+			try {
+				if ( sessions != null ) {
+					sessions.close();
+					sessions = null;
+				}
+			}
+			catch (Exception ignore) {
+			}
+			throw e;
+		}
+	}
+
+	public Session openSession() throws HibernateException {
+		session = getSessions().openSession();
+		return session;
+	}
+
+	protected abstract Class[] getMappings();
+
+	protected String[] getAnnotatedPackages() {
+		return new String[]{};
+	}
+
+	protected String[] getXmlFiles() {
+		return new String[]{};
+	}
+
+	private void setSessions(SessionFactory sessions) {
+		TestCase.sessions = sessions;
+	}
+
+	protected SessionFactory getSessions() {
+		return sessions;
+	}
+
+	private void setDialect(Dialect dialect) {
+		TestCase.dialect = dialect;
+	}
+
+	protected Dialect getDialect() {
+		return dialect;
+	}
+
+	protected static void setCfg(AnnotationConfiguration cfg) {
+		TestCase.cfg = cfg;
+	}
+
+	protected static AnnotationConfiguration getCfg() {
+		return cfg;
+	}
+
+	protected void configure(Configuration cfg) {
+		//cfg.setNamingStrategy( AlternativeNamingStrategy.INSTANCE );
+		//cfg.getSessionEventListenerConfig().setFlushEventListener( new EJB3FlushEventListener() );
+		//cfg.getSessionEventListenerConfig().setAutoFlushEventListener( new EJB3AutoFlushEventListener() );
+	}
+
+	protected boolean recreateSchema() {
+		return true;
+	}
+
+}




More information about the hibernate-commits mailing list