HHH-8794 Support for @OrderBy without specifying attribute

This commit is contained in:
Matus Zamborsky 2013-12-12 12:34:39 +01:00 committed by Brett Meyer
parent 3de5e65b63
commit a2fcfc602b
5 changed files with 264 additions and 10 deletions

View File

@ -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 );
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<Asset2> assets = new ArrayList<Asset2>();
/** */
@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<Asset2> getAssets() {
return assets;
}
}

View File

@ -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
};
}
}