Author: stliu
Date: 2011-02-14 02:45:36 -0500 (Mon, 14 Feb 2011)
New Revision: 20911
Added:
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Contractor.java
Modified:
annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employee.java
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employer.java
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
Log:
JBPAPP-5930 HHH-4394 @OrderBy is used in conjunction with a table per class hierarchy
produces incorred SQL syntax.
Modified:
annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
---
annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/annotations/CollectionBinder.java 2011-02-14
06:55:59 UTC (rev 20910)
+++
annotations/branches/v3_3_1_GA_CP/src/java/org/hibernate/cfg/annotations/CollectionBinder.java 2011-02-14
07:45:36 UTC (rev 20911)
@@ -80,6 +80,7 @@
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
import org.hibernate.mapping.SimpleValue;
+import org.hibernate.mapping.SingleTableSubclass;
import org.hibernate.mapping.Table;
import org.hibernate.util.StringHelper;
@@ -886,12 +887,14 @@
//TODO check whether @ManyToOne @JoinTable in @IdClass used for @OrderBy works:
doh!
table = "";
}
- else if ( pc != associatedClass ) {
+ else if (pc == associatedClass
+ || (associatedClass instanceof SingleTableSubclass && pc
+ .getMappedClass().isAssignableFrom(
+ associatedClass.getMappedClass()))) {
+ table = "";
+ } else {
table = pc.getTable().getQuotedName() + ".";
}
- else {
- table = "";
- }
Iterator propertyColumns = p.getColumnIterator();
while ( propertyColumns.hasNext() ) {
Selectable column = (Selectable) propertyColumns.next();
Added:
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Contractor.java
===================================================================
---
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Contractor.java
(rev 0)
+++
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Contractor.java 2011-02-14
07:45:36 UTC (rev 20911)
@@ -0,0 +1,26 @@
+//$Id: Employee.java 14736 2008-06-04 14:23:42Z hardy.ferentschik $
+package org.hibernate.test.annotations.manytomany;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+
+/**
+ * Employee in an Employer-Employee relationship
+ *
+ * @author Emmanuel Bernard
+ */
+@Entity
+@SuppressWarnings("serial")
+public class Contractor extends Employee implements Serializable {
+
+ private float hourlyRate;
+
+ public float getHourlyRate() {
+ return hourlyRate;
+ }
+
+ public void setHourlyRate(float hourlyRate) {
+ this.hourlyRate = hourlyRate;
+ }
+}
Modified:
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employee.java
===================================================================
---
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employee.java 2011-02-14
06:55:59 UTC (rev 20910)
+++
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employee.java 2011-02-14
07:45:36 UTC (rev 20911)
@@ -7,6 +7,8 @@
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.Column;
@@ -17,7 +19,8 @@
*
* @author Emmanuel Bernard
*/
-@Entity()
+@Entity
+@Inheritance(strategy = InheritanceType.JOINED)
public class Employee implements Serializable {
private Integer id;
private Collection<Employer> employers;
Modified:
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employer.java
===================================================================
---
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employer.java 2011-02-14
06:55:59 UTC (rev 20910)
+++
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/Employer.java 2011-02-14
07:45:36 UTC (rev 20911)
@@ -3,6 +3,8 @@
import java.io.Serializable;
import java.util.Collection;
+import java.util.List;
+
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@@ -25,8 +27,24 @@
public class Employer implements Serializable {
private Integer id;
private Collection employees;
+ private List contractors;
@ManyToMany(
+ targetEntity = org.hibernate.test.annotations.manytomany.Contractor.class,
+ cascade = {CascadeType.PERSIST, CascadeType.MERGE}
+ )
+ @JoinTable(
+ name = "EMPLOYER_CONTRACTOR",
+ joinColumns = {@JoinColumn(name = "EMPLOYER_ID")},
+ inverseJoinColumns = {@JoinColumn(name = "CONTRACTOR_ID")}
+ )
+ @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
+ @OrderBy("name desc")
+ public List getContractors() {
+ return contractors;
+ }
+
+ @ManyToMany(
targetEntity = org.hibernate.test.annotations.manytomany.Employee.class,
cascade = {CascadeType.PERSIST, CascadeType.MERGE}
)
@@ -36,7 +54,7 @@
inverseJoinColumns = {@JoinColumn(name = "EMPEE_ID")}
)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
- @OrderBy("name")
+ @OrderBy("name asc")
public Collection getEmployees() {
return employees;
}
@@ -46,6 +64,10 @@
public Integer getId() {
return id;
}
+
+ public void setContractors(List contractors) {
+ this.contractors = contractors;
+ }
public void setEmployees(Collection set) {
employees = set;
Modified:
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
===================================================================
---
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2011-02-14
06:55:59 UTC (rev 20910)
+++
annotations/branches/v3_3_1_GA_CP/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2011-02-14
07:45:36 UTC (rev 20911)
@@ -7,6 +7,7 @@
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.List;
import java.util.Set;
import org.hibernate.Hibernate;
@@ -218,44 +219,116 @@
s.close();
}
- public void testOrderBy() throws Exception {
+ public void testOrderByEmployee() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
- Employer er = new Employer();
- Employee ee = new Employee();
- ee.setName( "Emmanuel" );
- Employee ee2 = new Employee();
- ee2.setName( "Alice" );
- s.persist( ee );
- s.persist( ee2 );
+ Employer employer = new Employer();
+ Employee employee1 = new Employee();
+ employee1.setName( "Emmanuel" );
+ Employee employee2 = new Employee();
+ employee2.setName( "Alice" );
+ s.persist( employee1 );
+ s.persist( employee2 );
Set erColl = new HashSet();
Collection eeColl = new ArrayList();
Collection eeColl2 = new ArrayList();
- erColl.add( ee );
- erColl.add( ee2 );
- eeColl.add( er );
- eeColl2.add( er );
- er.setEmployees( erColl );
- ee.setEmployers( eeColl );
- ee2.setEmployers( eeColl2 );
- //s.persist(ee);
+ erColl.add( employee1 );
+ erColl.add( employee2 );
+ eeColl.add( employer );
+ eeColl2.add( employer );
+ employer.setEmployees( erColl );
+ employee1.setEmployers( eeColl );
+ employee2.setEmployers( eeColl2 );
s.flush();
s.clear();
- er = (Employer) s.get( Employer.class, er.getId() );
- assertNotNull( er );
- assertNotNull( er.getEmployees() );
- assertEquals( 2, er.getEmployees().size() );
- Employee eeFromDb = (Employee) er.getEmployees().iterator().next();
- assertEquals( ee2.getName(), eeFromDb.getName() );
+ employer = (Employer) s.get( Employer.class, employer.getId() );
+ assertNotNull( employer );
+ assertNotNull( employer.getEmployees() );
+ assertEquals( 2, employer.getEmployees().size() );
+ Employee eeFromDb = (Employee) employer.getEmployees().iterator().next();
+ assertEquals( employee2.getName(), eeFromDb.getName() );
tx.rollback();
s.close();
}
+ /**
+ * ANN-625
+ *
+ * @throws Exception in case the test fails.
+ *
+ * This test only works against databases which allow a mixed usage of
+ * table names and table aliases. The generated SQL for this test is:
+ *
+ * select
+ * contractor0_.EMPLOYER_ID as EMPLOYER1_1_,
+ * contractor0_.CONTRACTOR_ID as CONTRACTOR2_1_,
+ * contractor1_.id as id2_0_,
+ * contractor1_.fld_name as fld3_2_0_,
+ * contractor1_.hourlyRate as hourlyRate2_0_
+ * from
+ * EMPLOYER_CONTRACTOR contractor0_
+ * left outer join
+ * Employee contractor1_
+ * on contractor0_.CONTRACTOR_ID=contractor1_.id
+ * where
+ * contractor0_.EMPLOYER_ID=?
+ * order by
+ * Employee.fld_name desc
+ *
+ *
+ */
+// HHH-3577
+ public void testOrderByContractor() throws Exception {
+ Session s;
+ Transaction tx;
+ s = openSession();
+ tx = s.beginTransaction();
+
+ // create some test entities
+ Employer employer = new Employer();
+ Contractor contractor1 = new Contractor();
+ contractor1.setName( "Emmanuel" );
+ contractor1.setHourlyRate(100.0f);
+ Contractor contractor2 = new Contractor();
+ contractor2.setName( "Hardy" );
+ contractor2.setHourlyRate(99.99f);
+ s.persist( contractor1 );
+ s.persist( contractor2 );
+
+ // add contractors to employer
+ List setOfContractors = new ArrayList();
+ setOfContractors.add( contractor1 );
+ setOfContractors.add( contractor2 );
+ employer.setContractors( setOfContractors );
+
+ // add employer to contractors
+ Collection employerListContractor1 = new ArrayList();
+ employerListContractor1.add( employer );
+ contractor1.setEmployers( employerListContractor1 );
+
+ Collection employerListContractor2 = new ArrayList();
+ employerListContractor2.add( employer );
+ contractor2.setEmployers( employerListContractor2 );
+
+ s.flush();
+ s.clear();
+
+ // assertions
+ employer = (Employer) s.get( Employer.class, employer.getId() );
+ assertNotNull( employer );
+ assertNotNull( employer.getContractors() );
+ assertEquals( 2, employer.getContractors().size() );
+ Contractor firstContractorFromDb = (Contractor)
employer.getContractors().iterator().next();
+ assertEquals( contractor2.getName(), firstContractorFromDb.getName() );
+ tx.rollback();
+ s.close();
+ }
+
public void testRemoveInBetween() throws Exception {
Session s;
Transaction tx;
@@ -575,6 +648,7 @@
Friend.class,
Employer.class,
Employee.class,
+ Contractor.class,
Man.class,
Woman.class,
Store.class,