[hibernate-commits] Hibernate SVN: r18543 - core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Wed Jan 13 14:35:55 EST 2010
Author: smarlow at redhat.com
Date: 2010-01-13 14:35:54 -0500 (Wed, 13 Jan 2010)
New Revision: 18543
Added:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java
Modified:
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/Employee.java
core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
Log:
HHH-4685 Make sure bidirectional @*To* works from an embedded object to another entity
Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ContactInfo.java 2010-01-13 19:35:54 UTC (rev 18543)
@@ -0,0 +1,24 @@
+package org.hibernate.test.annotations.manytomany;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Embeddable;
+import javax.persistence.ManyToMany;
+import java.util.List;
+
+ at Embeddable
+public class ContactInfo {
+// @ManyToOne
+// Address address; // Unidirectional
+
+ List<PhoneNumber> phoneNumbers; // Bidirectional
+
+ @ManyToMany(cascade= CascadeType.ALL)
+ public List<PhoneNumber> getPhoneNumbers() {
+ return phoneNumbers;
+ }
+
+ public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
+ this.phoneNumbers = phoneNumbers;
+ }
+
+}
Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/Employee.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/Employee.java 2010-01-13 18:04:02 UTC (rev 18542)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/Employee.java 2010-01-13 19:35:54 UTC (rev 18543)
@@ -4,6 +4,7 @@
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
+import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@@ -26,7 +27,30 @@
private Integer id;
private Collection<Employer> employers;
private String name;
+ ContactInfo contactInfo;
+ JobInfo jobInfo;
+ // ContactInfo is for ManyToMany testing
+ @Embedded
+ public ContactInfo getContactInfo() {
+ return contactInfo;
+ }
+
+ public void setContactInfo(ContactInfo contactInfo) {
+ this.contactInfo = contactInfo;
+ }
+
+ // JobInfo is for OneToMany testing
+ @Embedded
+ public JobInfo getJobInfo() {
+ return jobInfo;
+ }
+
+ public void setJobInfo(JobInfo jobInfo) {
+ this.jobInfo = jobInfo;
+ }
+
+
@Column(name="fld_name")
public String getName() {
return name;
Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/JobInfo.java 2010-01-13 19:35:54 UTC (rev 18543)
@@ -0,0 +1,29 @@
+package org.hibernate.test.annotations.manytomany;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Embeddable;
+import javax.persistence.ManyToOne;
+
+ at Embeddable
+public class JobInfo {
+ String jobDescription;
+ ProgramManager pm; // Bidirectional
+
+ public String getJobDescription() {
+ return jobDescription;
+ }
+
+ public void setJobDescription( String jobDescription ) {
+ this.jobDescription = jobDescription;
+ }
+
+ @ManyToOne( cascade= CascadeType.ALL)
+ public ProgramManager getPm() {
+ return pm;
+ }
+
+ public void setPm( ProgramManager pm ) {
+ this.pm = pm;
+ }
+
+}
Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2010-01-13 18:04:02 UTC (rev 18542)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ManyToManyTest.java 2010-01-13 19:35:54 UTC (rev 18543)
@@ -1,4 +1,5 @@
//$Id$
+//$Id$
package org.hibernate.test.annotations.manytomany;
@@ -7,6 +8,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;
@@ -641,6 +643,81 @@
s.close();
}
+ // Test for HHH-4685
+ // Section 11.1.25
+ // The ManyToMany annotation may be used within an embeddable class contained within an entity class to specify a
+ // relationship to a collection of entities[101]. If the relationship is bidirectional and the entity containing
+ // the embeddable class is the owner of the relationship, the non-owning side must use the mappedBy element of the
+ // ManyToMany annotation to specify the relationship field or property of the embeddable class. The dot (".")
+ // notation syntax must be used in the mappedBy element to indicate the relationship attribute within the embedded
+ // attribute. The value of each identifier used with the dot notation is the name of the respective embedded field
+ // or property.
+ public void testManyToManyEmbeddableBiDirectionalDotNotationInMappedBy() throws Exception {
+ Session s;
+ Transaction tx;
+ s = openSession();
+ tx = s.beginTransaction();
+ Employee e = new Employee();
+ e.setName( "Sharon" );
+ List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
+ Collection<Employee> employees = new ArrayList<Employee>();
+ employees.add( e );
+ ContactInfo contactInfo = new ContactInfo();
+ PhoneNumber number = new PhoneNumber();
+ number.setEmployees( employees );
+ phoneNumbers.add( number );
+ contactInfo.setPhoneNumbers( phoneNumbers );
+ e.setContactInfo( contactInfo );
+ s.persist( e );
+ s.flush();
+ s.clear();
+ tx.commit();
+
+ tx.begin();
+ e = (Employee)s.get( e.getClass(),e.getId() );
+ // follow both directions of many to many association
+ assertEquals("same employee", e.getName(), e.getContactInfo().getPhoneNumbers().get(0).getEmployees().iterator().next().getName());
+ tx.commit();
+
+ s.close();
+ }
+
+ // Test for HHH-4685
+ // Section 11.1.26
+ // The ManyToOne annotation may be used within an embeddable class to specify a relationship from the embeddable
+ // class to an entity class. If the relationship is bidirectional, the non-owning OneToMany entity side must use the
+ // mappedBy element of the OneToMany annotation to specify the relationship field or property of the embeddable field
+ // or property on the owning side of the relationship. The dot (".") notation syntax must be used in the mappedBy
+ // element to indicate the relationship attribute within the embedded attribute. The value of each identifier used
+ // with the dot notation is the name of the respective embedded field or property.
+ public void testOneToManyEmbeddableBiDirectionalDotNotationInMappedBy() throws Exception {
+ Session s;
+ Transaction tx;
+ s = openSession();
+ tx = s.beginTransaction();
+ Employee e = new Employee();
+ JobInfo job = new JobInfo();
+ job.setJobDescription( "Sushi Chef" );
+ ProgramManager pm = new ProgramManager();
+ Collection<Employee> employees = new ArrayList<Employee>();
+ employees.add(e);
+ pm.setManages( employees );
+ job.setPm(pm);
+ e.setJobInfo( job );
+ s.persist( e );
+ s.flush();
+ s.clear();
+ tx.commit();
+
+ tx.begin();
+ e = (Employee) s.get( e.getClass(), e.getId() );
+ assertEquals( "same job in both directions",
+ e.getJobInfo().getJobDescription(),
+ e.getJobInfo().getPm().getManages().iterator().next().getJobInfo().getJobDescription() );
+ tx.commit();
+ s.close();
+ }
+
/**
* @see org.hibernate.test.annotations.TestCase#getMappings()
*/
@@ -664,7 +741,9 @@
Inspector.class,
InspectorPrefixes.class,
BuildingCompany.class,
- Building.class
+ Building.class,
+ PhoneNumber.class,
+ ProgramManager.class
};
}
Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/PhoneNumber.java 2010-01-13 19:35:54 UTC (rev 18543)
@@ -0,0 +1,31 @@
+package org.hibernate.test.annotations.manytomany;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import java.util.Collection;
+
+ at Entity
+public class PhoneNumber {
+ int phNumber;
+ Collection<Employee> employees;
+
+ @Id
+ public int getPhNumber() {
+ return phNumber;
+ }
+
+ public void setPhNumber(int phNumber) {
+ this.phNumber = phNumber;
+ }
+
+ @ManyToMany(mappedBy="contactInfo.phoneNumbers", cascade= CascadeType.ALL)
+ public Collection<Employee> getEmployees() {
+ return employees;
+ }
+
+ public void setEmployees(Collection<Employee> employees) {
+ this.employees = employees;
+ }
+}
Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/manytomany/ProgramManager.java 2010-01-13 19:35:54 UTC (rev 18543)
@@ -0,0 +1,33 @@
+package org.hibernate.test.annotations.manytomany;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import java.util.Collection;
+
+ at Entity
+public class ProgramManager {
+ int id;
+
+ Collection<Employee> manages;
+
+ @Id
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ @OneToMany( mappedBy="jobInfo.pm", cascade= CascadeType.ALL )
+ public Collection<Employee> getManages() {
+ return manages;
+ }
+
+ public void setManages( Collection<Employee> manages ) {
+ this.manages = manages;
+ }
+
+}
More information about the hibernate-commits
mailing list