HHH-6627 HHH-7752 - Test cases

This commit is contained in:
Lukasz Antoniak 2012-12-18 12:49:46 +01:00 committed by Brett Meyer
parent 0a17d4612a
commit ceb96094f5
5 changed files with 277 additions and 9 deletions

View File

@ -0,0 +1,54 @@
package org.hibernate.test.dialect.functional;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
public class Category implements Serializable {
@Id
public Integer id;
public String name;
@OneToMany(mappedBy = "category")
public List<Product2> products;
public Category() {
}
public Category(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if ( this == o ) return true;
if ( ! ( o instanceof Category ) ) return false;
Category category = (Category) o;
if ( id != null ? !id.equals( category.id ) : category.id != null ) return false;
if ( name != null ? !name.equals( category.name ) : category.name != null ) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + ( name != null ? name.hashCode() : 0 );
return result;
}
@Override
public String toString() {
return "Category(id = " + id + ", name = " + name + ")";
}
}

View File

@ -0,0 +1,73 @@
package org.hibernate.test.dialect.functional;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Table(name = "contacts")
public class Contact implements Serializable {
@Id
@Column(name = "id")
public Long id;
@Column(name = "type")
public String type;
@Column(name = "firstname")
public String firstName;
@Column(name = "lastname")
public String lastName;
@ManyToOne
@JoinColumn(name = "folder_id")
public Folder folder;
public Contact() {
}
public Contact(Long id, String firstName, String lastName, String type, Folder folder) {
this.firstName = firstName;
this.folder = folder;
this.id = id;
this.lastName = lastName;
this.type = type;
}
@Override
public boolean equals(Object o) {
if ( this == o ) return true;
if ( ! ( o instanceof Contact ) ) return false;
Contact contact = (Contact) o;
if ( id != null ? !id.equals( contact.id ) : contact.id != null ) return false;
if ( firstName != null ? !firstName.equals( contact.firstName ) : contact.firstName != null ) return false;
if ( lastName != null ? !lastName.equals( contact.lastName ) : contact.lastName != null ) return false;
if ( type != null ? !type.equals( contact.type ) : contact.type != null ) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + ( type != null ? type.hashCode() : 0 );
result = 31 * result + ( firstName != null ? firstName.hashCode() : 0 );
result = 31 * result + ( lastName != null ? lastName.hashCode() : 0 );
return result;
}
@Override
public String toString() {
return "Contact(id = " + id + ", type = " + type + ", firstName = " + firstName + ", lastName = " + lastName + ")";
}
}

View File

@ -0,0 +1,58 @@
package org.hibernate.test.dialect.functional;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Formula;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@Entity
@Table(name = "folders")
public class Folder implements Serializable {
@Id
public Long id;
public String name;
@Formula("( SELECT CASE WHEN c.type = 'owner' THEN c.firstname + ' ' + c.lastname END FROM contacts c where c.folder_id = id )")
public String owner;
public Folder() {
}
public Folder(Long id, String name) {
this.id = id;
this.name = name;
}
@Override
public boolean equals(Object o) {
if ( this == o ) return true;
if ( ! ( o instanceof Folder ) ) return false;
Folder folder = (Folder) o;
if ( id != null ? !id.equals( folder.id ) : folder.id != null ) return false;
if ( name != null ? !name.equals( folder.name ) : folder.name != null ) return false;
if ( owner != null ? !owner.equals( folder.owner ) : folder.owner != null ) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + ( name != null ? name.hashCode() : 0 );
result = 31 * result + ( owner != null ? owner.hashCode() : 0 );
return result;
}
@Override
public String toString() {
return "Folder(id = " + id + ", name = " + name + ", owner = " + owner + ")";
}
}

View File

@ -32,6 +32,7 @@ import java.io.Serializable;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
/** /**
@ -45,6 +46,9 @@ public class Product2 implements Serializable {
@Column(name = "description", nullable = false) @Column(name = "description", nullable = false)
public String description; public String description;
@ManyToOne
public Category category;
public Product2() { public Product2() {
} }
@ -53,6 +57,12 @@ public class Product2 implements Serializable {
this.description = description; this.description = description;
} }
public Product2(Integer id, String description, Category category) {
this.category = category;
this.description = description;
this.id = id;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if ( this == o ) return true; if ( this == o ) return true;

View File

@ -26,29 +26,30 @@
*/ */
package org.hibernate.test.dialect.functional; package org.hibernate.test.dialect.functional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.Test;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.dialect.SQLServer2005Dialect; import org.hibernate.dialect.SQLServer2005Dialect;
import org.hibernate.exception.LockTimeoutException; import org.hibernate.exception.LockTimeoutException;
import org.hibernate.exception.SQLGrammarException;
import org.hibernate.jdbc.ReturningWork; import org.hibernate.jdbc.ReturningWork;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/** /**
* used driver hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver * used driver hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver
@ -215,6 +216,80 @@ public class SQLServerDialectTest extends BaseCoreFunctionalTestCase {
session.close(); session.close();
} }
@Test
@TestForIssue(jiraKey = "HHH-6627")
public void testPaginationWithAggregation() {
Session session = openSession();
Transaction tx = session.beginTransaction();
// populating test data
Category category1 = new Category( 1, "Category1" );
Category category2 = new Category( 2, "Category2" );
Category category3 = new Category( 3, "Category3" );
session.persist( category1 );
session.persist( category2 );
session.persist( category3 );
session.flush();
session.persist( new Product2( 1, "Kit1", category1 ) );
session.persist( new Product2( 2, "Kit2", category1 ) );
session.persist( new Product2( 3, "Kit3", category1 ) );
session.persist( new Product2( 4, "Kit4", category2 ) );
session.persist( new Product2( 5, "Kit5", category2 ) );
session.persist( new Product2( 6, "Kit6", category3 ) );
session.flush();
session.clear();
// count number of products in each category
List<Object[]> result = session.createCriteria( Category.class, "c" ).createAlias( "products", "p" )
.setProjection(
Projections.projectionList()
.add( Projections.groupProperty( "c.id" ) )
.add( Projections.countDistinct( "p.id" ) )
)
.addOrder( Order.asc( "c.id" ) )
.setFirstResult( 1 ).setMaxResults( 3 ).list();
assertEquals( 2, result.size() );
assertArrayEquals( new Object[] { 2, 2L }, result.get( 0 ) ); // two products of second category
assertArrayEquals( new Object[] { 3, 1L }, result.get( 1 ) ); // one products of third category
tx.rollback();
session.close();
}
@Test
@TestForIssue(jiraKey = "HHH-7752")
public void testPaginationWithFormulaSubquery() {
Session session = openSession();
Transaction tx = session.beginTransaction();
// populating test data
Folder folder1 = new Folder( 1L, "Folder1" );
Folder folder2 = new Folder( 2L, "Folder2" );
Folder folder3 = new Folder( 3L, "Folder3" );
session.persist( folder1 );
session.persist( folder2 );
session.persist( folder3 );
session.flush();
session.persist( new Contact( 1L, "Lukasz", "Antoniak", "owner", folder1 ) );
session.persist( new Contact( 2L, "Kinga", "Mroz", "co-owner", folder2 ) );
session.flush();
session.clear();
session.refresh( folder1 );
session.refresh( folder2 );
session.clear();
List<Long> folderCount = session.createQuery( "select count(distinct f) from Folder f" ).setMaxResults( 1 ).list();
assertEquals( Arrays.asList( 3L ), folderCount );
List<Folder> distinctFolders = session.createQuery( "select distinct f from Folder f order by f.id desc" )
.setFirstResult( 1 ).setMaxResults( 2 ).list();
assertEquals( Arrays.asList( folder2, folder1 ), distinctFolders );
tx.rollback();
session.close();
}
@Test @Test
@TestForIssue(jiraKey = "HHH-3961") @TestForIssue(jiraKey = "HHH-3961")
public void testLockNowaitSqlServer() throws Exception { public void testLockNowaitSqlServer() throws Exception {
@ -275,14 +350,12 @@ public class SQLServerDialectTest extends BaseCoreFunctionalTestCase {
s.getTransaction().begin(); s.getTransaction().begin();
s.delete( kit ); s.delete( kit );
s.getTransaction().commit(); s.getTransaction().commit();
} }
@Override @Override
protected java.lang.Class<?>[] getAnnotatedClasses() { protected java.lang.Class<?>[] getAnnotatedClasses() {
return new java.lang.Class[] { return new java.lang.Class[] {
Product2.class Product2.class, Category.class, Folder.class, Contact.class
}; };
} }
} }