HHH-6762: Skipped currently failing test and added new test that uses copy of test class that contains join formula suitable for SQL Server

This commit is contained in:
John Verhaeg 2011-10-24 11:11:35 -05:00
parent 007980d8bf
commit 71c230bf5b
2 changed files with 132 additions and 22 deletions

View File

@ -2,24 +2,24 @@
* Hibernate, Relational Persistence for Idiomatic Java * Hibernate, Relational Persistence for Idiomatic Java
* *
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third- * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-
* party contributors as indicated by the @author tags or express * party contributors as indicated by the @author tags or express
* copyright attribution statements applied by the authors. * copyright attribution statements applied by the authors.
* All third-party contributions are distributed under license by * All third-party contributions are distributed under license by
* Red Hat, Inc. * Red Hat, Inc.
* *
* This copyrighted material is made available to anyone wishing to * This copyrighted material is made available to anyone wishing to
* use, modify, copy, or redistribute it subject to the terms and * use, modify, copy, or redistribute it subject to the terms and
* conditions of the GNU Lesser General Public License, as published * conditions of the GNU Lesser General Public License, as published
* by the Free Software Foundation. * by the Free Software Foundation.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this distribution; if not, write to: * License along with this distribution; if not, write to:
* *
* Free Software Foundation, Inc. * Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
@ -31,6 +31,8 @@ import org.junit.Test;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.SQLServer2005Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -115,7 +117,7 @@ public class ManyToOneWithFormulaTest extends BaseCoreFunctionalTestCase {
public void testReferencedColumnNameBelongsToEmbeddedIdOfReferencedEntity() throws Exception { public void testReferencedColumnNameBelongsToEmbeddedIdOfReferencedEntity() throws Exception {
Session session = openSession(); Session session = openSession();
Transaction tx = session.beginTransaction(); Transaction tx = session.beginTransaction();
Integer companyCode = 10; Integer companyCode = 10;
Integer mfgCode = 100; Integer mfgCode = 100;
String contractNumber = "NSAR97841"; String contractNumber = "NSAR97841";
@ -126,7 +128,7 @@ public class ManyToOneWithFormulaTest extends BaseCoreFunctionalTestCase {
Model model = new Model(new ModelId(companyCode, mfgCode, "FOCUS"), Model model = new Model(new ModelId(companyCode, mfgCode, "FOCUS"),
"FORD FOCUS"); "FORD FOCUS");
session.persist(manufacturer); session.persist(manufacturer);
session.persist(model); session.persist(model);
@ -149,29 +151,29 @@ public class ManyToOneWithFormulaTest extends BaseCoreFunctionalTestCase {
tx.commit(); tx.commit();
session.close(); session.close();
} }
@Test @Test
@SkipForDialect(value = { HSQLDialect.class }, comment = "The used join conditions does not work in HSQLDB. See HHH-4497") @SkipForDialect(value = { HSQLDialect.class, SQLServer2005Dialect.class }, comment = "The used join conditions does not work in HSQLDB. See HHH-4497")
public void testManyToOneFromNonPkToNonPk() throws Exception { public void testManyToOneFromNonPkToNonPk() throws Exception {
// also tests usage of the stand-alone @JoinFormula annotation (i.e. not wrapped within @JoinColumnsOrFormulas) // also tests usage of the stand-alone @JoinFormula annotation (i.e. not wrapped within @JoinColumnsOrFormulas)
Session s = openSession(); Session s = openSession();
Transaction tx = s.beginTransaction(); Transaction tx = s.beginTransaction();
Product kit = new Product(); Product kit = new Product();
kit.id = 1; kit.id = 1;
kit.productIdnf = "KIT"; kit.productIdnf = "KIT";
kit.description = "Kit"; kit.description = "Kit";
s.persist(kit); s.persist(kit);
Product kitkat = new Product(); Product kitkat = new Product();
kitkat.id = 2; kitkat.id = 2;
kitkat.productIdnf = "KIT_KAT"; kitkat.productIdnf = "KIT_KAT";
kitkat.description = "Chocolate"; kitkat.description = "Chocolate";
s.persist(kitkat); s.persist(kitkat);
s.flush(); s.flush();
s.clear(); s.clear();
kit = (Product) s.get(Product.class, 1); kit = (Product) s.get(Product.class, 1);
kitkat = (Product) s.get(Product.class, 2); kitkat = (Product) s.get(Product.class, 2);
System.out.println(kitkat.description); System.out.println(kitkat.description);
@ -180,10 +182,45 @@ public class ManyToOneWithFormulaTest extends BaseCoreFunctionalTestCase {
assertEquals(kit.productIdnf, kitkat.getProductFamily().productIdnf); assertEquals(kit.productIdnf, kitkat.getProductFamily().productIdnf);
assertEquals("KIT_KAT", kitkat.productIdnf.trim()); assertEquals("KIT_KAT", kitkat.productIdnf.trim());
assertEquals("Chocolate", kitkat.description.trim()); assertEquals("Chocolate", kitkat.description.trim());
tx.rollback(); tx.rollback();
s.close(); s.close();
} }
@Test
@RequiresDialect(value = {SQLServer2005Dialect.class})
public void testManyToOneFromNonPkToNonPkSqlServer() throws Exception {
// also tests usage of the stand-alone @JoinFormula annotation (i.e. not wrapped within @JoinColumnsOrFormulas)
Session s = openSession();
Transaction tx = s.beginTransaction();
ProductSqlServer kit = new ProductSqlServer();
kit.id = 1;
kit.productIdnf = "KIT";
kit.description = "Kit";
s.persist(kit);
ProductSqlServer kitkat = new ProductSqlServer();
kitkat.id = 2;
kitkat.productIdnf = "KIT_KAT";
kitkat.description = "Chocolate";
s.persist(kitkat);
s.flush();
s.clear();
kit = (ProductSqlServer) s.get(ProductSqlServer.class, 1);
kitkat = (ProductSqlServer) s.get(ProductSqlServer.class, 2);
System.out.println(kitkat.description);
assertNotNull(kitkat);
assertEquals(kit, kitkat.getProductFamily());
assertEquals(kit.productIdnf, kitkat.getProductFamily().productIdnf);
assertEquals("KIT_KAT", kitkat.productIdnf.trim());
assertEquals("Chocolate", kitkat.description.trim());
tx.rollback();
s.close();
}
@Override @Override
protected java.lang.Class<?>[] getAnnotatedClasses() { protected java.lang.Class<?>[] getAnnotatedClasses() {
@ -200,7 +237,8 @@ public class ManyToOneWithFormulaTest extends BaseCoreFunctionalTestCase {
ModelId.class, ModelId.class,
Manufacturer.class, Manufacturer.class,
ManufacturerId.class, ManufacturerId.class,
Product.class Product.class,
ProductSqlServer.class
}; };
} }

View File

@ -0,0 +1,72 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2009, Red Hat, Inc. and/or its affiliates 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.manytoonewithformula;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.JoinFormula;
/**
* @author Sharath Reddy
*
*/
@Entity
@Table(name="product")
public class ProductSqlServer implements Serializable
{
private static final long serialVersionUID = 6956478993159505828L;
@Id
public Integer id;
@Column(name="product_idnf", length=18, nullable=false, unique=true,
columnDefinition="char(18)")
public String productIdnf;
@Column(name="description", nullable=false)
public String description;
@ManyToOne
@JoinFormula(value="SUBSTRING(product_idnf, 1, 3)",
referencedColumnName="product_idnf")
@Fetch(FetchMode.JOIN)
private ProductSqlServer productFamily;
public ProductSqlServer getProductFamily()
{
return productFamily;
}
}