HHH-2225 NPE when eager fetching joined component with native SQL query

This commit is contained in:
Strong Liu 2011-05-25 16:15:09 +08:00
parent 48ca1b285a
commit 27a33bc70f
2 changed files with 78 additions and 7 deletions

View File

@ -0,0 +1,45 @@
package org.hibernate.test.annotations.query;
import javax.persistence.Column;
import javax.persistence.ColumnResult;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
import org.hibernate.annotations.Formula;
@Entity
@Table(name = "ALL_TABLES")
@SqlResultSetMapping(name = "all",
entities = @EntityResult(entityClass = AllTables.class,
fields = {
@FieldResult(name = "tableName", column = "t_name"),
@FieldResult(name = "daysOld", column = "t_time")
}))
public class AllTables {
@Id
@Column(name = "TABLE_NAME", nullable = false)
private String tableName;
@Formula(value = "(SYSDATE())")
private String daysOld;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getDaysOld() {
return daysOld;
}
public void setDaysOld(String daysOld) {
this.daysOld = daysOld;
}
}

View File

@ -31,6 +31,7 @@ import java.util.List;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
@ -40,6 +41,7 @@ import org.hibernate.test.annotations.A320;
import org.hibernate.test.annotations.A320b; import org.hibernate.test.annotations.A320b;
import org.hibernate.test.annotations.Plane; import org.hibernate.test.annotations.Plane;
import org.hibernate.test.annotations.TestCase; import org.hibernate.test.annotations.TestCase;
import org.hibernate.testing.junit.FailureExpected;
import org.hibernate.testing.junit.SkipForDialect; import org.hibernate.testing.junit.SkipForDialect;
/** /**
@ -52,6 +54,29 @@ public class QueryAndSQLTest extends TestCase {
super( x ); super( x );
} }
public void testNativeQueryWithFormulaAttribute() {
String sql = "select t.table_name as {t.tableName}, sysdate() as {t.daysOld} from all_tables t where t.table_name = 'AUDIT_ACTIONS' ";
String sql2 = "select table_name as t_name, sysdate() as t_time from all_tables where table_name = 'AUDIT_ACTIONS' ";
Session s = openSession();
s.beginTransaction();
s.createSQLQuery( sql ).addEntity( "t", AllTables.class ).list();
s.createSQLQuery( sql2 ).setResultSetMapping( "all" ).list();
SQLQuery q = s.createSQLQuery( sql2 );
q.addRoot( "t", AllTables.class ).addProperty( "tableName", "t_name" ).addProperty( "daysOld", "t_time" );
q.list();
s.getTransaction().commit();
s.close();
}
@FailureExpected( jiraKey = "HHH-2225")
public void testNativeQueryWithFormulaAttributeWithoutAlias() {
String sql = "select table_name , sysdate() from all_tables where table_name = 'AUDIT_ACTIONS' ";
Session s = openSession();
s.beginTransaction();
s.createSQLQuery( sql ).addEntity( "t", AllTables.class ).list();
s.getTransaction().commit();
s.close();
}
public void testPackageQueries() throws Exception { public void testPackageQueries() throws Exception {
Session s = openSession(); Session s = openSession();
Transaction tx = s.beginTransaction(); Transaction tx = s.beginTransaction();
@ -412,7 +437,8 @@ public class QueryAndSQLTest extends TestCase {
SynonymousDictionary.class, SynonymousDictionary.class,
Captain.class, Captain.class,
Chaos.class, Chaos.class,
CasimirParticle.class CasimirParticle.class,
AllTables.class
}; };
} }