[hibernate-commits] Hibernate SVN: r18434 - in core/trunk/annotations/src: main/java/org/hibernate/cfg/annotations and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jan 7 12:41:06 EST 2010


Author: smarlow at redhat.com
Date: 2010-01-07 12:41:05 -0500 (Thu, 07 Jan 2010)
New Revision: 18434

Added:
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Bug.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/BugSystem.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Person.java
Modified:
   core/trunk/annotations/src/main/java/org/hibernate/cfg/BinderHelper.java
   core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
   core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/OrderByTest.java
Log:
HHH-4689  Make sure @OrderBy supports dotted notation to point to embedded properties

Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/BinderHelper.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/BinderHelper.java	2010-01-07 17:33:23 UTC (rev 18433)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/BinderHelper.java	2010-01-07 17:41:05 UTC (rev 18434)
@@ -368,6 +368,54 @@
 		return property;
 	}
 
+	/**
+	 * Retrieve the property by path in a recursive way
+	 */
+	public static Property findPropertyByName(Component component, String propertyName) {
+		Property property = null;
+		try {
+			if ( propertyName == null
+					|| propertyName.length() == 0) {
+				// Do not expect to use a primary key for this case
+				return null;
+			}
+			else {
+				StringTokenizer st = new StringTokenizer( propertyName, ".", false );
+				while ( st.hasMoreElements() ) {
+					String element = (String) st.nextElement();
+					if ( property == null ) {
+						property = component.getProperty( element );
+					}
+					else {
+						if ( !property.isComposite() ) return null;
+						property = ( (Component) property.getValue() ).getProperty( element );
+					}
+				}
+			}
+		}
+		catch (MappingException e) {
+			try {
+				//if we do not find it try to check the identifier mapper
+				if ( component.getOwner().getIdentifierMapper() == null ) return null;
+				StringTokenizer st = new StringTokenizer( propertyName, ".", false );
+				while ( st.hasMoreElements() ) {
+					String element = (String) st.nextElement();
+					if ( property == null ) {
+						property = component.getOwner().getIdentifierMapper().getProperty( element );
+					}
+					else {
+						if ( !property.isComposite() ) return null;
+						property = ( (Component) property.getValue() ).getProperty( element );
+					}
+				}
+			}
+			catch (MappingException ee) {
+				return null;
+			}
+		}
+		return property;
+	}
+
 	public static String getRelativePath(PropertyHolder propertyHolder, String propertyName) {
 		if ( propertyHolder == null ) return propertyName;
 		String path = propertyHolder.getPath();

Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java	2010-01-07 17:33:23 UTC (rev 18433)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java	2010-01-07 17:41:05 UTC (rev 18434)
@@ -1031,7 +1031,7 @@
 				int index = 0;
 
 				for (String property : properties) {
-					Property p = component.getProperty( property );
+					Property p = BinderHelper.findPropertyByName( component, property );
 					if ( p == null ) {
 						throw new AnnotationException(
 								"property from @OrderBy clause not found: "

Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Bug.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Bug.java	                        (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Bug.java	2010-01-07 17:41:05 UTC (rev 18434)
@@ -0,0 +1,35 @@
+package org.hibernate.test.annotations.collectionelement;
+
+import javax.persistence.Embeddable;
+
+ at Embeddable
+public class Bug {
+
+	private String description;
+	private Person reportedBy;
+	private String summary;
+
+	public String getSummary() {
+		return summary;
+	}
+
+	public void setSummary(String summary) {
+		this.summary = summary;
+	}
+
+	public Person getReportedBy() {
+		return reportedBy;
+	}
+
+	public void setReportedBy(Person reportedBy) {
+		this.reportedBy = reportedBy;
+	}
+
+	public String getDescription(){
+		return description;
+	}
+
+	public void setDescription(String description) {
+		this.description = description;
+	}
+}

Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/BugSystem.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/BugSystem.java	                        (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/BugSystem.java	2010-01-07 17:41:05 UTC (rev 18434)
@@ -0,0 +1,39 @@
+package org.hibernate.test.annotations.collectionelement;
+
+import javax.persistence.ElementCollection;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OrderBy;
+
+import java.util.Set;
+
+ at SuppressWarnings({"unchecked", "serial"})
+
+ at Entity
+public class BugSystem {
+	@Id
+	@GeneratedValue
+	private Integer id;
+
+	@ElementCollection
+	@OrderBy("reportedBy.lastName ASC,reportedBy.firstName ASC,summary")
+	private Set<Bug> bugs;
+
+	public Integer getId() {
+		return id;
+	}
+
+	public void setId(Integer id) {
+		this.id = id;
+	}
+
+	public Set<Bug> getBugs() {
+		return bugs;
+	}
+
+	public void setBugs(Set<Bug> bugs) {
+		this.bugs = bugs;
+	}
+
+}
\ No newline at end of file

Modified: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/OrderByTest.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/OrderByTest.java	2010-01-07 17:33:23 UTC (rev 18433)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/OrderByTest.java	2010-01-07 17:41:05 UTC (rev 18434)
@@ -5,7 +5,6 @@
 import org.hibernate.Transaction;
 import org.hibernate.test.annotations.TestCase;
 
-import javax.persistence.EntityManager;
 import java.util.HashSet;
 import java.util.Iterator;
 
@@ -53,10 +52,62 @@
 		s.close();
 	}
 
+	public void testOrderByWithDottedNotation() throws Exception {
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+
+		BugSystem bs = new BugSystem();
+		HashSet<Bug> set = new HashSet<Bug>();
+
+		Bug bug = new Bug();
+		bug.setDescription("JPA-2 locking");
+		bug.setSummary("JPA-2 impl locking");
+		Person p = new Person();
+		p.setFirstName("Scott");
+		p.setLastName("Marlow");
+		bug.setReportedBy(p);
+		set.add(bug);
+
+		bug = new Bug();
+		bug.setDescription("JPA-2 annotations");
+		bug.setSummary("JPA-2 impl annotations");
+		p = new Person();
+		p.setFirstName("Emmanuel");
+		p.setLastName("Bernard");
+		bug.setReportedBy(p);
+		set.add(bug);
+
+		bug = new Bug();
+		bug.setDescription("Implement JPA-2 criteria");
+		bug.setSummary("JPA-2 impl criteria");
+		p = new Person();
+		p.setFirstName("Steve");
+		p.setLastName("Ebersole");
+		bug.setReportedBy(p);
+		set.add(bug);
+
+		bs.setBugs(set);
+		s.persist(bs);
+		tx.commit();
+
+		tx = s.beginTransaction();
+		s.clear();
+		bs = (BugSystem) s.get(BugSystem.class,bs.getId());
+		Assert.assertTrue("has three bugs", bs.getBugs().size() == 3);
+		Iterator iter = bs.getBugs().iterator();
+		Assert.assertEquals( "Emmanuel", ((Bug)iter.next()).getReportedBy().getFirstName() );
+		Assert.assertEquals( "Steve", ((Bug)iter.next()).getReportedBy().getFirstName() );
+		Assert.assertEquals( "Scott", ((Bug)iter.next()).getReportedBy().getFirstName() );
+		tx.commit();
+		s.close();
+
+	}
+
 	protected Class[] getMappings() {
 		return new Class[] {
 			Products.class,
-			Widgets.class
+			Widgets.class,
+			BugSystem.class
 		};
 	}
 

Added: core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Person.java
===================================================================
--- core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Person.java	                        (rev 0)
+++ core/trunk/annotations/src/test/java/org/hibernate/test/annotations/collectionelement/Person.java	2010-01-07 17:41:05 UTC (rev 18434)
@@ -0,0 +1,26 @@
+package org.hibernate.test.annotations.collectionelement;
+
+import javax.persistence.Embeddable;
+
+ at Embeddable
+public class Person {
+
+	String lastName;
+	String firstName;
+
+	public String getLastName() {
+		return lastName;
+	}
+
+	public void setLastName(String lastName) {
+		this.lastName = lastName;
+	}
+
+	public String getFirstName() {
+		return firstName;
+	}
+
+	public void setFirstName(String firstName) {
+		this.firstName = firstName;
+	}
+}



More information about the hibernate-commits mailing list