From a2fcfc602b54b9f96d49efae7788f5d2266059f9 Mon Sep 17 00:00:00 2001 From: Matus Zamborsky Date: Thu, 12 Dec 2013 12:34:39 +0100 Subject: [PATCH] HHH-8794 Support for @OrderBy without specifying attribute --- .../cfg/annotations/CollectionBinder.java | 16 ++-- .../test/annotations/onetomany/Asset2.java | 80 +++++++++++++++++++ .../test/annotations/onetomany/Computer2.java | 67 ++++++++++++++++ .../test/annotations/onetomany/Employee2.java | 75 +++++++++++++++++ .../annotations/onetomany/OrderByTest.java | 36 ++++++++- 5 files changed, 264 insertions(+), 10 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Asset2.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Computer2.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Employee2.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java index 7c99308549..2132ae57dd 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java @@ -830,15 +830,13 @@ public abstract class CollectionBinder { PersistentClass associatedClass = (PersistentClass) persistentClasses.get( assocClass ); if ( jpaOrderBy != null ) { final String jpaOrderByFragment = jpaOrderBy.value(); - if ( StringHelper.isNotEmpty( jpaOrderByFragment ) ) { - final String orderByFragment = buildOrderByClauseFromHql( - jpaOrderBy.value(), - associatedClass, - collection.getRole() - ); - if ( StringHelper.isNotEmpty( orderByFragment ) ) { - collection.setOrderBy( orderByFragment ); - } + final String orderByFragment = buildOrderByClauseFromHql( + jpaOrderBy.value(), + associatedClass, + collection.getRole() + ); + if ( StringHelper.isNotEmpty( orderByFragment ) ) { + collection.setOrderBy( orderByFragment ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Asset2.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Asset2.java new file mode 100644 index 0000000000..0632536644 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Asset2.java @@ -0,0 +1,80 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.annotations.onetomany; + +import javax.persistence.*; +import java.io.Serializable; + +/** + * + */ +@Entity +@Inheritance(strategy = InheritanceType.JOINED) +public class Asset2 implements Serializable { + /** */ + @Id + @Column(name = "id_asset") + private final Integer idAsset; + @Id + @Column(name = "id_test") + private final Integer test; + /** */ + @ManyToOne(cascade = {CascadeType.ALL}) + @JoinColumn(nullable = false) + private Employee2 employee; + + public Asset2() { + this.idAsset = 0; + this.test = 1; + } + + /** + * @param idAsset + */ + public Asset2(Integer idAsset) { + this.idAsset = idAsset; + this.test = 1; + } + + /** + * @return the id + */ + public Integer getIdAsset() { + return idAsset; + } + + /** + * @return the employee + */ + public Employee2 getEmployee() { + return employee; + } + + /** + * @param employee the employee to set + */ + public void setEmployee(Employee2 employee) { + this.employee = employee; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Computer2.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Computer2.java new file mode 100644 index 0000000000..36f00bb48d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Computer2.java @@ -0,0 +1,67 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.annotations.onetomany; + +import javax.persistence.Entity; +import javax.persistence.PrimaryKeyJoinColumn; +import javax.persistence.PrimaryKeyJoinColumns; + +/** + * + */ +@Entity +@PrimaryKeyJoinColumns({ + @PrimaryKeyJoinColumn(name = "id_asset"), + @PrimaryKeyJoinColumn(name = "id_test") +}) +public class Computer2 + extends Asset2 { + /** */ + private String computerName; + + public Computer2() { + + } + + /** + * @param id + */ + public Computer2(Integer id) { + super(id); + } + + /** + * @return the computerName + */ + public String getComputerName() { + return computerName; + } + + /** + * @param computerName the computerName to set + */ + public void setComputerName(String computerName) { + this.computerName = computerName; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Employee2.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Employee2.java new file mode 100644 index 0000000000..ad4874a586 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/Employee2.java @@ -0,0 +1,75 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.annotations.onetomany; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +/** + * + */ +@Entity +public class Employee2 { + /** */ + @OneToMany(cascade = CascadeType.ALL, mappedBy = "employee", orphanRemoval = true) + @OrderBy + private final List assets = new ArrayList(); + /** */ + @Id + @Column(name = "id") + private Integer id; + + public Employee2() { + + } + + /** + * @param id + */ + public Employee2(Integer id) { + this.id = id; + } + + /** + * @return the id + */ + public Integer getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * @return the assets + */ + public List getAssets() { + return assets; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/OrderByTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/OrderByTest.java index 3cf9942035..5353dc56d4 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/OrderByTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetomany/OrderByTest.java @@ -409,6 +409,39 @@ public class OrderByTest extends BaseCoreFunctionalTestCase { assertEquals( 1, forum.getUsers().size() ); assertEquals( "john", forum.getUsers().get( 0 ).getName() ); } + + @Test + @TestForIssue(jiraKey = "HHH-8794") + public void testOrderByOnJoinedClassEmpty() { + + final Session s = openSession(); + s.getTransaction().begin(); + + Employee2 employee = new Employee2(1); + + Computer2 computer = new Computer2(1); + computer.setComputerName("Bob's computer"); + computer.setEmployee(employee); + + Computer2 computer2 = new Computer2(2); + computer2.setComputerName("Alice's computer"); + computer2.setEmployee(employee); + + s.save(employee); + s.save(computer2); + s.save(computer); + + s.flush(); + s.clear(); + sessionFactory().getCache().evictEntityRegions(); + + employee = (Employee2) s.get(Employee2.class, employee.getId()); + + assertEquals(2, employee.getAssets().size()); + assertEquals(1, employee.getAssets().get(0).getIdAsset().intValue()); + assertEquals(2, employee.getAssets().get(1).getIdAsset().intValue()); + } + @Override protected Class[] getAnnotatedClasses() { @@ -416,7 +449,8 @@ public class OrderByTest extends BaseCoreFunctionalTestCase { Order.class, OrderItem.class, Zoo.class, Tiger.class, Monkey.class, Visitor.class, Box.class, Item.class, BankAccount.class, Transaction.class, - Comment.class, Forum.class, Post.class, User.class + Comment.class, Forum.class, Post.class, User.class, + Asset2.class, Computer2.class, Employee2.class }; } }