📄 sqlfunctionstest.java
字号:
//$Id: SQLFunctionsTest.java,v 1.1.2.6 2004/02/04 20:08:55 oneovthafew Exp $package org.hibernate.test;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.List;import junit.framework.Test;import junit.framework.TestSuite;import junit.textui.TestRunner;import net.sf.hibernate.Hibernate;import net.sf.hibernate.Query;import net.sf.hibernate.ScrollableResults;import net.sf.hibernate.Session;import net.sf.hibernate.Transaction;import net.sf.hibernate.dialect.DB2Dialect;import net.sf.hibernate.dialect.HSQLDialect;import net.sf.hibernate.dialect.InterbaseDialect;import net.sf.hibernate.dialect.MckoiDialect;import net.sf.hibernate.dialect.MySQLDialect;import net.sf.hibernate.dialect.Oracle9Dialect;import net.sf.hibernate.dialect.PostgreSQLDialect;import net.sf.hibernate.dialect.SybaseDialect;import net.sf.hibernate.dialect.OracleDialect;public class SQLFunctionsTest extends TestCase { public SQLFunctionsTest(String arg0) { super(arg0); } public void testDialectSQLFunctions() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Iterator iter = s.iterate("select max(s.count) from s in class Simple"); if ( getDialect() instanceof MySQLDialect ) assertTrue( iter.hasNext() && iter.next()==null ); Simple simple = new Simple(); simple.setName("Simple Dialect Function Test"); simple.setAddress("Simple Address"); simple.setPay(new Float(45.8)); simple.setCount(2); s.save(simple, new Long(10) ); // Test to make sure allocating an specified object operates correctly. assertTrue( s.find("select new org.hibernate.test.S(s.count, s.address) from s in class Simple").size() == 1 ); // Quick check the base dialect functions operate correctly assertTrue( s.find("select max(s.count) from s in class Simple").size() == 1 ); assertTrue( s.find("select count(*) from s in class Simple").size() == 1 ); if ( getDialect() instanceof OracleDialect) { // Check Oracle Dialect mix of dialect functions - no args (no parenthesis and single arg functions java.util.List rset = s.find("select s.name, sysdate, trunc(s.pay), round(s.pay) from s in class Simple"); assertNotNull("Name string should have been returned",(((Object[])rset.get(0))[0])); assertNotNull("Todays Date should have been returned",(((Object[])rset.get(0))[1])); assertEquals("trunc(45.8) result was incorrect ", new Float(45), ( (Object[]) rset.get(0) )[2] ); assertEquals("round(45.8) result was incorrect ", new Float(46), ( (Object[]) rset.get(0) )[3] ); simple.setPay(new Float(-45.8)); s.update(simple); // Test type conversions while using nested functions (Float to Int). rset = s.find("select abs(round(s.pay)) from s in class Simple"); assertEquals("abs(round(-45.8)) result was incorrect ", new Integer(46), rset.get(0)); // Test a larger depth 3 function example - Not a useful combo other than for testing assertTrue( s.find("select trunc(round(sysdate)) from s in class Simple").size() == 1 ); } s.delete(simple); t.commit(); s.close(); } public void testSetProperties() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save(simple, new Long(10) ); Query q = s.createQuery("from s in class Simple where s.name=:name and s.count=:count"); q.setProperties(simple); assertTrue( q.list().get(0)==simple ); s.delete(simple); t.commit(); s.close(); } public void testBroken() throws Exception { if (getDialect() instanceof Oracle9Dialect) return; Session s = openSession(); Transaction t = s.beginTransaction(); Broken b = new Fixed(); b.setId( new Long(123)); b.setOtherId("foobar"); s.save(b); s.flush(); b.setTimestamp( new Date() ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update(b); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); b = (Broken) s.load( Broken.class, b ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.delete(b); t.commit(); s.close(); } public void testNothinToUpdate() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save( simple, new Long(10) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple, new Long(10) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple, new Long(10) ); s.delete(simple); t.commit(); s.close(); } public void testCachedQuery() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save( simple, new Long(10) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Query q = s.createQuery("from Simple s where s.name=?"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); q = s.createQuery("from Simple s where s.name=:name"); q.setCacheable(true); q.setString("name", "Simple 1"); assertTrue( q.list().size()==1 ); simple = (Simple) q.list().get(0); q.setString("name", "Simple 2"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); simple.setName("Simple 2"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); q = s.createQuery("from Simple s where s.name=:name"); q.setString("name", "Simple 2"); q.setCacheable(true); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); s.update( simple, new Long(10) ); s.delete(simple); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); q = s.createQuery("from Simple s where s.name=?"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); t.commit(); s.close(); } public void testCachedQueryOnInsert() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save( simple, new Long(10) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Query q = s.createQuery("from Simple s"); List list = q.setCacheable(true).list(); assertTrue( list.size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); q = s.createQuery("from Simple s"); list = q.setCacheable(true).list(); assertTrue( list.size()==1 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Simple simple2 = new Simple(); simple2.setCount(133); s.save( simple2, new Long(12) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); q = s.createQuery("from Simple s"); list = q.setCacheable(true).list(); assertTrue( list.size()==2 ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); q = s.createQuery("from Simple s"); list = q.setCacheable(true).list(); assertTrue( list.size()==2 ); Iterator i = list.iterator(); while ( i.hasNext() ) s.delete( i.next() ); t.commit(); s.close(); } public void testCachedQueryRegion() throws Exception { Session s = openSession(); Transaction t = s.beginTransaction(); Simple simple = new Simple(); simple.setName("Simple 1"); s.save( simple, new Long(10) ); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Query q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -