HHH-5715 test case
This commit is contained in:
parent
c0352429db
commit
55c822c56e
|
@ -0,0 +1,66 @@
|
|||
package org.hibernate.test.pagination;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.dialect.SQLServerDialect;
|
||||
import org.hibernate.testing.junit.functional.FunctionalTestCase;
|
||||
|
||||
/**
|
||||
* HHH-5715 bug test case: Dublicated entries when using select distinct with join and pagination. The bug has to do
|
||||
* with new {@link SQLServerDialect} that uses row_number function for pagination
|
||||
*
|
||||
* @author Valotasios Yoryos
|
||||
*
|
||||
*/
|
||||
public class DistinctSelectTest extends FunctionalTestCase {
|
||||
private static final int NUM_OF_USERS = 30;
|
||||
|
||||
public DistinctSelectTest(String string) {
|
||||
super(string);
|
||||
}
|
||||
|
||||
public String[] getMappings() {
|
||||
return new String[] { "pagination/EntryTag.hbm.xml" };
|
||||
}
|
||||
|
||||
public void feedDatabase() {
|
||||
List<Tag> tags = new ArrayList<Tag>();
|
||||
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
Tag tag = new Tag("Tag: " + UUID.randomUUID().toString());
|
||||
tags.add(tag);
|
||||
s.save(tag);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_OF_USERS; i++) {
|
||||
Entry e = new Entry("Entry: " + UUID.randomUUID().toString());
|
||||
e.getTags().addAll(tags);
|
||||
s.save(e);
|
||||
}
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
public void testDistinctSelectWithJoin() {
|
||||
feedDatabase();
|
||||
|
||||
Session s = openSession();
|
||||
|
||||
List<Entry> entries = s.createQuery("select distinct e from Entry e join e.tags t where t.surrogate != null order by e.name").setFirstResult(10).setMaxResults(5).list();
|
||||
|
||||
// System.out.println(entries);
|
||||
Entry firstEntry = entries.remove(0);
|
||||
assertFalse("The list of entries should not contain dublicated Entry objects as we've done a distinct select", entries.contains(firstEntry));
|
||||
|
||||
assertEquals(5, entries.size());
|
||||
|
||||
s.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package org.hibernate.test.pagination;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Entry {
|
||||
private int id;
|
||||
private String name;
|
||||
private Set<Tag> tags = new HashSet<Tag>();
|
||||
|
||||
public Entry() {
|
||||
|
||||
}
|
||||
|
||||
public Entry(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<Tag> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Set<Tag> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (getClass() != obj.getClass()) return false;
|
||||
Entry other = (Entry) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null) return false;
|
||||
}
|
||||
else if (!name.equals(other.name)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.pagination">
|
||||
|
||||
<class name="Tag" dynamic-update="true">
|
||||
<id name="id">
|
||||
<generator class="increment" />
|
||||
</id>
|
||||
<property name="surrogate" />
|
||||
</class>
|
||||
|
||||
<class name="Entry" dynamic-update="true">
|
||||
<id name="id">
|
||||
<generator class="increment" />
|
||||
</id>
|
||||
<property name="name" />
|
||||
<set name="tags" table="entry_tag">
|
||||
<key column="entry_id" />
|
||||
<many-to-many column="tag_id" class="org.hibernate.test.pagination.Tag" />
|
||||
</set>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,31 @@
|
|||
package org.hibernate.test.pagination;
|
||||
|
||||
public class Tag {
|
||||
private int id;
|
||||
private String surrogate;
|
||||
|
||||
public Tag() {
|
||||
|
||||
}
|
||||
|
||||
public Tag(String surrogate) {
|
||||
this.surrogate = surrogate;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSurrogate() {
|
||||
return surrogate;
|
||||
}
|
||||
|
||||
public void setSurrogate(String surrogate) {
|
||||
this.surrogate = surrogate;
|
||||
}
|
||||
|
||||
}
|
|
@ -21,10 +21,11 @@
|
|||
# 51 Franklin Street, Fifth Floor
|
||||
# Boston, MA 02110-1301 USA
|
||||
#
|
||||
hibernate.dialect org.hibernate.dialect.H2Dialect
|
||||
hibernate.connection.driver_class org.h2.Driver
|
||||
hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE
|
||||
hibernate.connection.username sa
|
||||
hibernate.dialect org.hibernate.dialect.SQLServer2008Dialect
|
||||
hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver
|
||||
hibernate.connection.url jdbc:jtds:sqlserver://localhost/test
|
||||
hibernate.connection.username tester
|
||||
hibernate.connection.password tester
|
||||
|
||||
hibernate.connection.pool_size 5
|
||||
|
||||
|
|
Loading…
Reference in New Issue