我遵循了spring.net文档和northwind示例。
BereikDao
[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>();
}
}
的HibernateDAO
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>();
}
}
弹簧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。 Nhibernate bug?
从调试:
Spring.Data.NHibernate.HibernateTransactionManager - Exposing Hibernate transaction as ADO transaction [System.Data.Odbc.OdbcConnection]
答案 0 :(得分:0)
让我知道如果你做这件事后仍然会出现错误。
首先将对象引用添加到Dao.XML并将其绑定到SessionFactory,如下所示:
<object id="BereikDao" type="Spring.Northwind.Dao.NHibernate.HibernateBereikDao, Spring.Northwind.Dao.NHibernate">
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>
您还必须将对象引用添加到Service.xml并将其绑定到BereikDao,如下所示:
<object id="BereikService" type="Spring.Northwind.Service.BereikService, Spring.Northwind.Service">
<property name="BereikDao" ref="BereikDao"/>
</object>