Author: stliu
Date: 2012-07-22 09:46:43 -0400 (Sun, 22 Jul 2012)
New Revision: 21042
Added:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/AllTables.java
Modified:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java
Log:
JBPAPP-6571 HHH-2536/HHH-2225 NPE in SQLQuery when Entity has @Formula attribute
Added:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/AllTables.java
===================================================================
---
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/AllTables.java
(rev 0)
+++
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/AllTables.java 2012-07-22
13:46:43 UTC (rev 21042)
@@ -0,0 +1,44 @@
+package org.hibernate.test.annotations.query;
+import javax.persistence.Column;
+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;
+ }
+}
\ No newline at end of file
Modified:
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java
===================================================================
---
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java 2012-07-22
13:08:20 UTC (rev 21041)
+++
annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java 2012-07-22
13:46:43 UTC (rev 21042)
@@ -7,10 +7,14 @@
import java.util.List;
import org.hibernate.Query;
+import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.PostgreSQLDialect;
+import org.hibernate.dialect.function.SQLFunction;
+import org.hibernate.engine.SessionFactoryImplementor;
+import org.hibernate.junit.FailureExpected;
import org.hibernate.junit.SkipForDialect;
import org.hibernate.stat.Statistics;
import org.hibernate.test.annotations.A320;
@@ -28,6 +32,43 @@
super( x );
}
+ public void testNativeQueryWithFormulaAttribute() {
+ SQLFunction dateFunction = getDialect().getFunctions().get( "current_date"
);
+ String dateFunctionRendered = dateFunction.render(
+ java.util.Collections.EMPTY_LIST,
+ (SessionFactoryImplementor)getSessions()
+ );
+
+ String sql = String.format(
+ "select t.table_name as {t.tableName}, %s as {t.daysOld} from ALL_TABLES t
where t.table_name = 'AUDIT_ACTIONS' ",
+ dateFunctionRendered
+ );
+ String sql2 = String.format(
+ "select table_name as t_name, %s as t_time from ALL_TABLES where table_name =
'AUDIT_ACTIONS' ",
+ dateFunctionRendered
+ );
+
+
+ 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.list();
+ s.getTransaction().commit();
+ s.close();
+ }
+ @FailureExpected(jiraKey = "JBPAPP-6571")
+ 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 {
Session s = openSession();
Transaction tx = s.beginTransaction();
@@ -364,7 +405,8 @@
SynonymousDictionary.class,
Captain.class,
Chaos.class,
- CasimirParticle.class
+ CasimirParticle.class,
+ AllTables.class
};
}
Show replies by date