Author: hardy.ferentschik
Date: 2008-06-12 09:41:16 -0400 (Thu, 12 Jun 2008)
New Revision: 14764
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Contractor.java
Modified:
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employee.java
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employer.java
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
Log:
ANN-625:
- Added test case. Needs to be run against other db than HSQL. Produced query is also
dodgy in HSQL, but seems not to cause a problem.
Added:
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Contractor.java
===================================================================
--- annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Contractor.java
(rev 0)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Contractor.java 2008-06-12
13:41:16 UTC (rev 14764)
@@ -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;
+ }
+}
Property changes on:
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Contractor.java
___________________________________________________________________
Name: svn:eol-style
+ native
Modified:
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employee.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employee.java 2008-06-12
00:16:50 UTC (rev 14763)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employee.java 2008-06-12
13:41:16 UTC (rev 14764)
@@ -17,7 +17,8 @@
*
* @author Emmanuel Bernard
*/
-@Entity()
+@Entity
+@SuppressWarnings("serial")
public class Employee implements Serializable {
private Integer id;
private Collection<Employer> employers;
Modified:
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employer.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employer.java 2008-06-12
00:16:50 UTC (rev 14763)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/Employer.java 2008-06-12
13:41:16 UTC (rev 14764)
@@ -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;
@@ -22,11 +24,32 @@
*/
@Entity()
@Table(name="`Employer`")
+@SuppressWarnings({"serial", "unchecked"})
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;
+ }
+
+ public void setContractors(List contractors) {
+ this.contractors = contractors;
+ }
+
+ @ManyToMany(
targetEntity = org.hibernate.test.annotations.manytomany.Employee.class,
cascade = {CascadeType.PERSIST, CascadeType.MERGE}
)
@@ -36,7 +59,7 @@
inverseJoinColumns = {@JoinColumn(name = "EMPEE_ID")}
)
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
- @OrderBy("name")
+ @OrderBy("name asc")
public Collection getEmployees() {
return employees;
}
Modified:
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
===================================================================
---
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2008-06-12
00:16:50 UTC (rev 14763)
+++
annotations/trunk/src/test/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2008-06-12
13:41:16 UTC (rev 14764)
@@ -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;
@@ -20,6 +21,7 @@
*
* @author Emmanuel Bernard
*/
+@SuppressWarnings("unchecked")
public class ManyToManyTest extends TestCase {
public ManyToManyTest(String x) {
@@ -218,44 +220,115 @@
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 fails test fails for other databases (except HSQL) due to missing alias in order
by clause:
+ *
+ * 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
+ *
+ *
+ */
+ 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,
@@ -590,7 +664,6 @@
InspectorPrefixes.class,
BuildingCompany.class,
Building.class
-
};
}