我遵循了 spring.net 文档和北风示例。
贝瑞克道
[Repository]
public class BereikDao : HibernateDao, IBereikDao
{
[Transaction(ReadOnly = true)]
public model.Bereik getById(int id)
{
return CurrentSession.Get<Bereik>(id);
}
[Transaction(ReadOnly = true)]
public IList<Bereik> getAll()
{
return getAll<Bereik>();
}
}
休眠道
public abstract class HibernateDao
{
private ISessionFactory sessionFactory;
/// <summary>
/// Session factory for sub-classes.
/// </summary>
public ISessionFactory SessionFactory
{
protected get { return sessionFactory; }
set { sessionFactory = value; }
}
/// <summary>
/// Get's the current active session. Will retrieve session as managed by the
/// Open Session In View module if enabled.
/// </summary>
protected ISession CurrentSession
{
get { return sessionFactory.GetCurrentSession(); }
}
protected IList<T> getAll<T>() where T : class
{
ICriteria criteria = CurrentSession.CreateCriteria<T>();
return criteria.List<T>();
}
}
spring-dao.xml
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database"
xmlns:tx="http://www.springframework.net/tx">
<db:provider id="DbProvider"
provider="IfxOdbc" connectionString="Dsn=${db.dsn};Host=${db.host};Server=${db.server};Service=${db.service};Database=${db.database};UID=${db.user};Password=${db.password};"/>
<object id="appConfigPropertyHolder"
type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="configSections" value="databaseSettings"/>
</object>
<object id="NHibernateSessionFactory" type="Demo.dao.CustomLocalSessionFactoryObject">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Demo</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect" value="NHibernate.Dialect.InformixDialect1000"/>
<entry key="connection.driver_class" value="NHibernate.Driver.IfxDriver"/>
<entry key="show_sql" value="true" />
</dictionary>
</property>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
<object id="transactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate33">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>
<object type="Spring.Dao.Attributes.PersistenceExceptionTranslationPostProcessor, Spring.Data"/>
<object id="bereikDao" type="Demo.dao.BereikDao">
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>
<object id="bereikService" type="Demo.service.impl.BereikService">
<property name="bereikDao" ref="bereikDao"/>
</object>
<tx:attribute-driven transaction-manager="transactionManager" proxy-target-type="true"/>
</objects>
BereikService
public class BereikService : IBereikService
{
private IBereikDao _bereikDao;
public IBereikDao bereikDao
{
get { return _bereikDao; }
set { _bereikDao = value; }
}
[Transaction]
public IList<model.Bereik> getAll()
{
return _bereikDao.getAll();
}
}
在我的应用程序中:
bereikService = (BereikService)ctx.GetObject("bereikService");
IList<Bereik> bereiken = bereikService.getAll();
在 bereikService 中调试 bereikDao 时为空。
突然出现以下错误:
'Errors' node cannot be resolved for the specified context [System.InvalidCastException: Kan een object van het type System.Data.Odbc.OdbcConnection niet converteren naar het type IBM.Data.Informix.IfxConnection.
bij IBM.Data.Informix.IfxCommand.System.Data.IDbCommand.set_Connection(IDbConnection value)
bij NHibernate.AdoNet.AbstractBatcher.Prepare(IDbCommand cmd) in p:\nhibernate-core\src\NHibernate\AdoNet\AbstractBatcher.cs:regel 112
bij NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd) in p:\nhibernate-core\src\NHibernate\AdoNet\AbstractBatcher.cs:regel 217
bij NHibernate.Loader.Loader.GetResultSet(IDbCommand st, Boolean autoDiscoverTypes, Boolean callable, RowSelection selection, ISessionImplementor session) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:regel 1226
bij NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:regel 421
bij NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:regel 251
bij NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:regel 1564].
翻译:无法将 System.Data.Odbc.OdbcConnection 的对象转换为 IBM.Data.Informix.IfxConnection。休眠错误?
从调试:
Spring.Data.NHibernate.HibernateTransactionManager - Exposing Hibernate transaction as ADO transaction [System.Data.Odbc.OdbcConnection]