HHH-4689 Make sure @OrderBy supports dotted notation to point to embedded properties

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18434 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Scott Marlow 2010-01-07 17:41:05 +00:00
parent a96f7bf729
commit 9ef2317e28
6 changed files with 202 additions and 3 deletions

View File

@ -368,6 +368,54 @@ public class BinderHelper {
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();

View File

@ -1031,7 +1031,7 @@ public abstract class CollectionBinder {
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: "

View File

@ -0,0 +1,35 @@
package org.hibernate.test.annotations.collectionelement;
import javax.persistence.Embeddable;
@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;
}
}

View File

@ -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;
@SuppressWarnings({"unchecked", "serial"})
@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;
}
}

View File

@ -5,7 +5,6 @@ import org.hibernate.Session;
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 @@ public class OrderByTest extends TestCase {
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
};
}

View File

@ -0,0 +1,26 @@
package org.hibernate.test.annotations.collectionelement;
import javax.persistence.Embeddable;
@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;
}
}