Author: steve.ebersole(a)jboss.com
Date: 2006-12-05 09:55:02 -0500 (Tue, 05 Dec 2006)
New Revision: 10923
Removed:
trunk/Hibernate3/test/org/hibernate/test/propertyref/Group.java
trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.hbm.xml
trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.java
trunk/Hibernate3/test/org/hibernate/test/propertyref/PropertyRefTest.java
Log:
HHH-2282 : PersistentClass propetty lookups and embedded composite identifiers;
consolidate property-ref tests
Deleted: trunk/Hibernate3/test/org/hibernate/test/propertyref/Group.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/propertyref/Group.java 2006-12-05 14:40:29
UTC (rev 10922)
+++ trunk/Hibernate3/test/org/hibernate/test/propertyref/Group.java 2006-12-05 14:55:02
UTC (rev 10923)
@@ -1,23 +0,0 @@
-//$Id$
-package org.hibernate.test.propertyref;
-
-import java.util.HashSet;
-import java.util.Set;
-
-public class Group {
- private String name;
- private Set users = new HashSet();
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Set getUsers() {
- return users;
- }
- public void setUsers(Set users) {
- this.users = users;
- }
-}
Deleted: trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.hbm.xml 2006-12-05
14:40:29 UTC (rev 10922)
+++ trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.hbm.xml 2006-12-05
14:55:02 UTC (rev 10923)
@@ -1,76 +0,0 @@
-<?xml version="1.0"?>
-<!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
-
-<!--
-
- Demonstrates the use of property-ref to map legacy data where
- foreign keys reference something other than the primary key of
- the associated entity. Here we show:
-
- (1) A one-to-one foreign key association (prefer primary key
- associations)
-
- (2) A bidirectional one-to-many association on a key that is
- not the primary key (prefer associations from foreign keys
- to primary keys)
-
--->
-
-<hibernate-mapping package="org.hibernate.test.propertyref">
-
- <class name="Person">
- <id name="id">
- <generator class="hilo"/>
- </id>
-
- <property name="name" length="100"/>
- <property name="userId" column="person_userid"
length="8" unique="true"/>
-
- <one-to-one name="address" property-ref="person"
cascade="all" fetch="join"/>
-
- <set name="accounts" inverse="true">
- <key column="userId" property-ref="userId"/>
- <one-to-many class="Account"/>
- </set>
-
- <bag name="systems" table="USER_SYSTEM"
lazy="false" inverse="false" cascade="all">
- <key column="USER_ID" property-ref="userId" />
- <element type="string" column="SYSTEM" />
- </bag>
- </class>
-
- <class name="Address">
- <id name="id">
- <generator class="hilo"/>
- </id>
-
- <property name="address" length="300"/>
- <property name="zip" length="5"/>
- <property name="country" length="25"/>
- <many-to-one name="person" unique="true"
not-null="true"/>
- </class>
-
- <class name="Account">
- <id name="accountId" length="32">
- <generator class="uuid.hex"/>
- </id>
-
- <many-to-one name="user"
- column="userId"
- property-ref="userId"/>
-
- <property name="type" not-null="true"/>
-
- </class>
-
- <class name="Group" table="`Group`">
- <id name="name"/>
- <set name="users" table="UserGroup"
cascade="save-update">
- <key column="groupName"/>
- <many-to-many column="userId" class="Person"
property-ref="userId"/>
- </set>
- </class>
-
-</hibernate-mapping>
\ No newline at end of file
Deleted: trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.java 2006-12-05 14:40:29
UTC (rev 10922)
+++ trunk/Hibernate3/test/org/hibernate/test/propertyref/Person.java 2006-12-05 14:55:02
UTC (rev 10923)
@@ -1,88 +0,0 @@
-//$Id$
-package org.hibernate.test.propertyref;
-
-import java.util.HashSet;
-import java.util.Set;
-import java.util.List;
-import java.util.ArrayList;
-
-/**
- * @author gavin
- */
-public class Person {
- private Long id;
- private String name;
- private Address address;
- private String userId;
- private Set accounts = new HashSet();
- private List systems = new ArrayList();
-
- /**
- * @return Returns the userId.
- */
- public String getUserId() {
- return userId;
- }
- /**
- * @param userId The userId to set.
- */
- public void setUserId(String userId) {
- this.userId = userId;
- }
- /**
- * @return Returns the address.
- */
- public Address getAddress() {
- return address;
- }
- /**
- * @param address The address to set.
- */
- public void setAddress(Address address) {
- this.address = address;
- }
- /**
- * @return Returns the id.
- */
- public Long getId() {
- return id;
- }
- /**
- * @param id The id to set.
- */
- public void setId(Long id) {
- this.id = id;
- }
- /**
- * @return Returns the name.
- */
- public String getName() {
- return name;
- }
- /**
- * @param name The name to set.
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return Returns the accounts.
- */
- public Set getAccounts() {
- return accounts;
- }
- /**
- * @param accounts The accounts to set.
- */
- public void setAccounts(Set accounts) {
- this.accounts = accounts;
- }
-
- public List getSystems() {
- return systems;
- }
-
- public void setSystems(List systems) {
- this.systems = systems;
- }
-}
Deleted: trunk/Hibernate3/test/org/hibernate/test/propertyref/PropertyRefTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/propertyref/PropertyRefTest.java 2006-12-05
14:40:29 UTC (rev 10922)
+++ trunk/Hibernate3/test/org/hibernate/test/propertyref/PropertyRefTest.java 2006-12-05
14:55:02 UTC (rev 10923)
@@ -1,269 +0,0 @@
-//$Id$
-package org.hibernate.test.propertyref;
-
-import java.util.Iterator;
-import java.util.List;
-
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
-import org.hibernate.FetchMode;
-import org.hibernate.Hibernate;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.cfg.Environment;
-import org.hibernate.mapping.Column;
-import org.hibernate.mapping.ForeignKey;
-import org.hibernate.mapping.PersistentClass;
-import org.hibernate.test.TestCase;
-
-/**
- * @author Gavin King
- */
-public class PropertyRefTest extends TestCase {
-
- public PropertyRefTest(String str) {
- super(str);
- }
-
- public void testNonLazyBagKeyPropertyRef() {
- Session s = openSession();
- Transaction t = s.beginTransaction();
- Person p = new Person();
- p.setName( "Steve" );
- p.setUserId( "steve" );
- p.getSystems().add( "QA" );
- p.getSystems().add( "R&D" );
- s.persist( p );
- t.commit();
- s.close();
-
- s = openSession();
- t = s.beginTransaction();
- s.createQuery( "from Person" ).list();
- s.clear();
- s.createSQLQuery( "select {p.*} from Person {p}" )
- .addEntity( "p", Person.class.getName() )
- .list();
- t.commit();
- s.close();
-
- s = openSession();
- t = s.beginTransaction();
- List results = s.createQuery( "from Person" ).list();
- Iterator itr = results.iterator();
- while ( itr.hasNext() ) {
- s.delete( itr.next() );
- }
- t.commit();
- s.close();
- }
-
- public void testManyToManyPropertyRef() {
- // prepare some test data relating to the Group->Person many-to-many association
- Session s = openSession();
- Transaction t = s.beginTransaction();
- Person p = new Person();
- p.setName( "Steve" );
- p.setUserId( "steve" );
- s.persist( p );
- Group g = new Group();
- g.setName( "Admins" );
- g.getUsers().add( p );
- s.persist( g );
- // force a flush and detachment here to test reattachment handling of the property-ref
(HHH-1531)
- t.commit();
- s.close();
-
- Person p2 = new Person();
- p2.setName( "Max" );
- p2.setUserId( "max" );
- g.getUsers().add( p2 );
-
- s = openSession();
- t = s.beginTransaction();
- s.update( g );
- t.commit();
- s.close();
-
- // test retrieval of the group
- s = openSession();
- t = s.beginTransaction();
- g = ( Group ) s.createQuery( "from Group g left join fetch g.users"
).uniqueResult();
- assertTrue( Hibernate.isInitialized( g.getUsers() ) );
- assertEquals( 2, g.getUsers().size() );
- s.delete( g );
- s.createQuery( "delete Person" ).executeUpdate();
- t.commit();
- s.close();
- }
-
- public void testOneToOnePropertyRef() {
- Session s = openSession();
- Transaction t = s.beginTransaction();
- Person p = new Person();
- p.setName("Steve");
- p.setUserId("steve");
- Address a = new Address();
- a.setAddress("Texas");
- a.setCountry("USA");
- p.setAddress(a);
- a.setPerson(p);
- s.save(p);
- Person p2 = new Person();
- p2.setName("Max");
- p2.setUserId("max");
- s.save(p2);
- Account act = new Account();
- act.setType('c');
- act.setUser(p2);
- p2.getAccounts().add(act);
- s.save(act);
- s.flush();
- s.clear();
-
- p = (Person) s.get( Person.class, p.getId() ); //get address reference by outer join
- p2 = (Person) s.get( Person.class, p2.getId() ); //get null address reference by outer
join
- assertNull( p2.getAddress() );
- assertNotNull( p.getAddress() );
- List l = s.createQuery("from Person").list(); //pull address references for
cache
- assertEquals( l.size(), 2 );
- assertTrue( l.contains(p) && l.contains(p2) );
- s.clear();
-
- l = s.createQuery("from Person p order by p.name").list(); //get address
references by sequential selects
- assertEquals( l.size(), 2 );
- assertNull( ( (Person) l.get(0) ).getAddress() );
- assertNotNull( ( (Person) l.get(1) ).getAddress() );
- s.clear();
-
- l = s.createQuery("from Person p left join fetch p.address a order by
a.country").list(); //get em by outer join
- assertEquals( l.size(), 2 );
- if ( ( (Person) l.get(0) ).getName().equals("Max") ) {
- assertNull( ( (Person) l.get(0) ).getAddress() );
- assertNotNull( ( (Person) l.get(1) ).getAddress() );
- }
- else {
- assertNull( ( (Person) l.get(1) ).getAddress() );
- assertNotNull( ( (Person) l.get(0) ).getAddress() );
- }
- s.clear();
-
- l = s.createQuery("from Person p left join p.accounts a").list();
- for ( int i=0; i<2; i++ ) {
- Object[] row = (Object[]) l.get(i);
- Person px = (Person) row[0];
- assertFalse( Hibernate.isInitialized( px.getAccounts() ) );
- assertTrue( px.getAccounts().size()>0 || row[1]==null );
- }
- s.clear();
-
- l = s.createQuery("from Person p left join fetch p.accounts a order by
p.name").list();
- Person p0 = (Person) l.get(0);
- assertTrue( Hibernate.isInitialized( p0.getAccounts() ) );
- assertEquals( p0.getAccounts().size(), 1 );
- assertSame( ( (Account) p0.getAccounts().iterator().next() ).getUser(), p0 );
- Person p1 = (Person) l.get(1);
- assertTrue( Hibernate.isInitialized( p1.getAccounts() ) );
- assertEquals( p1.getAccounts().size(), 0 );
- s.clear();
- Account acc = (Account) s.createQuery("from Account a left join fetch
a.user").uniqueResult();
- assertTrue( Hibernate.isInitialized(acc.getUser()) );
- assertNotNull(acc.getUser());
- assertTrue( acc.getUser().getAccounts().contains(acc) );
-
- s.createQuery("delete from Address").executeUpdate();
- s.createQuery("delete from Account").executeUpdate(); // to not break
constraint violation between Person and Account
- s.createQuery("delete from Person").executeUpdate();
-
- t.commit();
- s.close();
- }
-
-
- public void testJoinFetchPropertyRef() {
- Session s = openSession();
- Transaction t = s.beginTransaction();
- Person p = new Person();
- p.setName("Steve");
- p.setUserId("steve");
- Address a = new Address();
- a.setAddress("Texas");
- a.setCountry("USA");
- p.setAddress(a);
- a.setPerson(p);
- s.save(p);
-
- s.flush();
- s.clear();
-
- getSessions().getStatistics().clear();
-
- p = (Person) s.get( Person.class, p.getId() ); //get address reference by outer join
-
- assertTrue( Hibernate.isInitialized( p.getAddress() ) );
- assertNotNull( p.getAddress() );
- assertEquals( getSessions().getStatistics().getPrepareStatementCount(), 1 );
- assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
-
- s.clear();
-
- getSessions().getStatistics().clear();
-
- p = (Person) s.createCriteria(Person.class)
- .setFetchMode("address", FetchMode.SELECT)
- .uniqueResult(); //get address reference by select
-
- assertTrue( Hibernate.isInitialized( p.getAddress() ) );
- assertNotNull( p.getAddress() );
- assertEquals( getSessions().getStatistics().getPrepareStatementCount(), 2 );
- assertEquals( getSessions().getStatistics().getEntityFetchCount(), 0 );
-
- s.createQuery("delete from Address").executeUpdate();
- s.createQuery("delete from Person").executeUpdate();
-
- t.commit();
- s.close();
- }
-
- public void testForeignKeyCreation() {
- PersistentClass classMapping =
getCfg().getClassMapping("org.hibernate.test.propertyref.Account");
-
- Iterator foreignKeyIterator = classMapping.getTable().getForeignKeyIterator();
- boolean found = false;
- while ( foreignKeyIterator.hasNext() ) {
- ForeignKey element = (ForeignKey) foreignKeyIterator.next();
- if(element.getReferencedEntityName().equals(Person.class.getName() ) ) {
-
- if(!element.isReferenceToPrimaryKey() ) {
- List referencedColumns = element.getReferencedColumns();
- Column column = (Column) referencedColumns.get(0);
- if(column.getName().equals("person_userid") ) {
- found = true; // extend test to include the columns
- }
- }
- }
- }
-
- assertTrue("Property ref foreign key not found",found);
- }
-
- protected String[] getMappings() {
- return new String[] { "propertyref/Person.hbm.xml" };
- }
-
- public static Test suite() {
- return new TestSuite(PropertyRefTest.class);
- }
-
- protected void configure(Configuration cfg) {
- cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "1");
- cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
- }
-
- public String getCacheConcurrencyStrategy() {
- return null;
- }
-}
-
Show replies by date