下面列出了怎么用org.hibernate.dialect.SQLServerDialect的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Determine the Hibernate database dialect class for the given target database.
* @param database the target database
* @return the Hibernate database dialect class, or {@code null} if none found
*/
@SuppressWarnings("deprecation")
protected Class<?> determineDatabaseDialectClass(Database database) {
switch (database) {
case DB2: return DB2Dialect.class;
case DERBY: return DerbyDialect.class; // DerbyDialect deprecated in 4.x
case H2: return H2Dialect.class;
case HSQL: return HSQLDialect.class;
case INFORMIX: return InformixDialect.class;
case MYSQL: return MySQLDialect.class;
case ORACLE: return Oracle9iDialect.class;
case POSTGRESQL: return PostgreSQLDialect.class; // PostgreSQLDialect deprecated in 4.x
case SQL_SERVER: return SQLServerDialect.class;
case SYBASE: return org.hibernate.dialect.SybaseDialect.class; // SybaseDialect deprecated in 3.6 but not 4.x
default: return null;
}
}
public static EngineDecorator getEngineDecorator(String dialect) throws ClassNotFoundException {
Class<?> dialectClass = Class.forName(dialect);
if (MySQLDialect.class.isAssignableFrom(dialectClass)) {
return new MySQLDecorator();
} else if (PostgreSQL81Dialect.class.isAssignableFrom(dialectClass)) {
return new PostgreSQLDecorator();
} else if (Oracle8iDialect.class.isAssignableFrom(dialectClass)) {
return new OracleDecorator();
} else if (SQLServerDialect.class.isAssignableFrom(dialectClass)) {
return new SQLServerDecorator();
}
return new NoOpDecorator();
}
public void testStr() {
Session session = openSession();
Transaction txn = session.beginTransaction();
Animal an = new Animal();
an.setBodyWeight(123.45f);
session.persist(an);
String str = (String) session.createQuery("select str(an.bodyWeight) from Animal an where str(an.bodyWeight) like '123%' or str(an.bodyWeight) like '1.23%'").uniqueResult();
if ( getDialect() instanceof DB2Dialect ) {
assertTrue( str.startsWith("1.234") );
}
else if ( getDialect() instanceof SQLServerDialect ) {
// no assertion as SQLServer always returns nulls here; even trying directly against the
// database, it seems to have problems with str() in the where clause...
}
else {
assertTrue( str.startsWith("123.4") );
}
if ( ! ( getDialect() instanceof SybaseDialect ) ) {
// In TransactSQL (the variant spoken by Sybase and SQLServer), the str() function
// is explicitly intended for numeric values only...
String dateStr1 = (String) session.createQuery("select str(current_date) from Animal").uniqueResult();
String dateStr2 = (String) session.createQuery("select str(year(current_date))||'-'||str(month(current_date))||'-'||str(day(current_date)) from Animal").uniqueResult();
System.out.println(dateStr1 + '=' + dateStr2);
if ( ! ( getDialect() instanceof Oracle9Dialect || getDialect() instanceof Oracle8iDialect ) ) { //Oracle renders the name of the month :(
String[] dp1 = StringHelper.split("-", dateStr1);
String[] dp2 = StringHelper.split("-", dateStr2);
for (int i=0; i<3; i++) {
if ( dp1[i].startsWith( "0" ) ) {
dp1[i] = dp1[i].substring( 1 );
}
assertEquals( dp1[i], dp2[i] );
}
}
}
session.delete(an);
txn.commit();
session.close();
}
private void addDialects(StrategySelectorImpl strategySelector) {
addDialect( strategySelector, Cache71Dialect.class );
addDialect( strategySelector, CUBRIDDialect.class );
addDialect( strategySelector, DB2Dialect.class );
addDialect( strategySelector, DB2390Dialect.class );
addDialect( strategySelector, DB2390V8Dialect.class );
addDialect( strategySelector, DB2400Dialect.class );
addDialect( strategySelector, DerbyTenFiveDialect.class );
addDialect( strategySelector, DerbyTenSixDialect.class );
addDialect( strategySelector, DerbyTenSevenDialect.class );
addDialect( strategySelector, FirebirdDialect.class );
addDialect( strategySelector, FrontBaseDialect.class );
addDialect( strategySelector, H2Dialect.class );
addDialect( strategySelector, HANAColumnStoreDialect.class );
addDialect( strategySelector, HANARowStoreDialect.class );
addDialect( strategySelector, HSQLDialect.class );
addDialect( strategySelector, InformixDialect.class );
addDialect( strategySelector, IngresDialect.class );
addDialect( strategySelector, Ingres9Dialect.class );
addDialect( strategySelector, Ingres10Dialect.class );
addDialect( strategySelector, InterbaseDialect.class );
addDialect( strategySelector, JDataStoreDialect.class );
addDialect( strategySelector, MckoiDialect.class );
addDialect( strategySelector, MimerSQLDialect.class );
addDialect( strategySelector, MySQL5Dialect.class );
addDialect( strategySelector, MySQL5InnoDBDialect.class );
addDialect( strategySelector, MySQL57InnoDBDialect.class );
addDialect( strategySelector, MySQL57Dialect.class );
addDialect( strategySelector, Oracle8iDialect.class );
addDialect( strategySelector, Oracle9iDialect.class );
addDialect( strategySelector, Oracle10gDialect.class );
addDialect( strategySelector, PointbaseDialect.class );
addDialect( strategySelector, PostgresPlusDialect.class );
addDialect( strategySelector, PostgreSQL81Dialect.class );
addDialect( strategySelector, PostgreSQL82Dialect.class );
addDialect( strategySelector, PostgreSQL9Dialect.class );
addDialect( strategySelector, ProgressDialect.class );
addDialect( strategySelector, SAPDBDialect.class );
addDialect( strategySelector, SQLServerDialect.class );
addDialect( strategySelector, SQLServer2005Dialect.class );
addDialect( strategySelector, SQLServer2008Dialect.class );
addDialect( strategySelector, Sybase11Dialect.class );
addDialect( strategySelector, SybaseAnywhereDialect.class );
addDialect( strategySelector, SybaseASE15Dialect.class );
addDialect( strategySelector, SybaseASE157Dialect.class );
addDialect( strategySelector, TeradataDialect.class );
addDialect( strategySelector, TimesTenDialect.class );
}
public void testStaleVersionedInstanceFoundOnLock() {
if ( ! readCommittedIsolationMaintained( "repeatable read tests" ) ) {
return;
}
if ( getDialect().doesReadCommittedCauseWritersToBlockReaders()) {
reportSkip( "lock blocking", "stale versioned instance" );
return;
}
String check = "EJB3 Specification";
Session s1 = getSessions().openSession();
Transaction t1 = s1.beginTransaction();
Item item = new Item( check );
s1.save( item );
t1.commit();
s1.close();
Long itemId = item.getId();
long initialVersion = item.getVersion();
// Now, open a new Session and re-load the item...
s1 = getSessions().openSession();
t1 = s1.beginTransaction();
item = ( Item ) s1.get( Item.class, itemId );
// now that the item is associated with the persistence-context of that session,
// open a new session and modify it "behind the back" of the first session
Session s2 = getSessions().openSession();
Transaction t2 = s2.beginTransaction();
Item item2 = ( Item ) s2.get( Item.class, itemId );
item2.setName( "EJB3 Persistence Spec" );
t2.commit();
s2.close();
// at this point, s1 now contains stale data, so acquire a READ lock
// and make sure we get the already associated state (i.e., the old
// name and the old version)
s1.lock( item, LockMode.READ );
item2 = ( Item ) s1.get( Item.class, itemId );
assertTrue( item == item2 );
assertEquals( "encountered non-repeatable read", check, item2.getName() );
assertEquals( "encountered non-repeatable read", initialVersion, item2.getVersion() );
// attempt to acquire an UPGRADE lock; this should fail
try {
s1.lock( item, LockMode.UPGRADE );
fail( "expected UPGRADE lock failure" );
}
catch( StaleObjectStateException expected ) {
// this is the expected behavior
}
catch( SQLGrammarException t ) {
if ( getDialect() instanceof SQLServerDialect ) {
// sql-server (using snapshot isolation) reports this as a grammar exception /:)
//
// not to mention that it seems to "lose track" of the transaction in this scenario...
t1.rollback();
t1 = s1.beginTransaction();
}
else {
throw t;
}
}
t1.commit();
s1.close();
// clean up
s1 = getSessions().openSession();
t1 = s1.beginTransaction();
s1.createQuery( "delete Item" ).executeUpdate();
t1.commit();
s1.close();
}
public void testLoadSaveRepository() throws Exception {
Connection theConnection = createConnection();
Class theHibernateDialect = SQLServerDialect.class;
String theModelResource = "/de/erdesignerng/test/io/repository/examplemodel.mxm";
String theNewFile = RepositioryHelper.performRepositorySaveAndLoad(theModelResource, theHibernateDialect,
theConnection);
String theOriginalFile = IOUtils.toString(getClass().getResourceAsStream(theModelResource));
assertTrue(compareStrings(theOriginalFile, theNewFile));
}