diff --git a/hibernate-testsuite/src/test/java/org/hibernate/test/annotations/query/AllTables.java b/hibernate-testsuite/src/test/java/org/hibernate/test/annotations/query/AllTables.java new file mode 100644 index 0000000000..5baa020ee4 --- /dev/null +++ b/hibernate-testsuite/src/test/java/org/hibernate/test/annotations/query/AllTables.java @@ -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; + } + } \ No newline at end of file diff --git a/hibernate-testsuite/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java b/hibernate-testsuite/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java index 7b2c004383..26d9e795a9 100644 --- a/hibernate-testsuite/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java +++ b/hibernate-testsuite/src/test/java/org/hibernate/test/annotations/query/QueryAndSQLTest.java @@ -31,6 +31,7 @@ import java.util.List; import org.hibernate.MappingException; import org.hibernate.Query; +import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.Transaction; 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.Plane; import org.hibernate.test.annotations.TestCase; +import org.hibernate.testing.junit.FailureExpected; import org.hibernate.testing.junit.SkipForDialect; /** @@ -52,6 +54,29 @@ public class QueryAndSQLTest extends TestCase { 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 { Session s = openSession(); Transaction tx = s.beginTransaction(); @@ -136,16 +161,16 @@ public class QueryAndSQLTest extends TestCase { s.close(); } - + /** * We are testing 2 things here: * 1. The query 'night.olderThan' is defined in a MappedSuperClass - Darkness. - * We are verifying that queries defined in a MappedSuperClass are processed. - * 2. There are 2 Entity classes that extend from Darkness - Night and Twilight. - * We are verifying that this does not cause any issues.eg. Double processing of the + * We are verifying that queries defined in a MappedSuperClass are processed. + * 2. There are 2 Entity classes that extend from Darkness - Night and Twilight. + * We are verifying that this does not cause any issues.eg. Double processing of the * MappedSuperClass */ - + public void testImportQueryFromMappedSuperclass() { Session s = openSession(); try { @@ -156,7 +181,7 @@ public class QueryAndSQLTest extends TestCase { } s.close(); } - + public void testSQLQueryWithManyToOne() { Statistics stats = getSessions().getStatistics(); stats.clear(); @@ -412,7 +437,8 @@ public class QueryAndSQLTest extends TestCase { SynonymousDictionary.class, Captain.class, Chaos.class, - CasimirParticle.class + CasimirParticle.class, + AllTables.class }; }