gpt4 book ai didi

c# - Spring.net Nhibernate dao 为空

转载 作者:行者123 更新时间:2023-11-30 18:35:36 25 4
gpt4 key购买 nike

我遵循了 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>();
}
}

休眠

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>

服务

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();

调试bereikDao时bereikService为null。

突然出现如下错误:

'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]

最佳答案

如果您在执行此操作后仍然遇到错误,请告诉我。

首先将对象引用添加到 Dao.XML 并将其绑定(bind)到 SessionFactory,如下所示:

<object id="BereikDao" type="Spring.Northwind.Dao.NHibernate.HibernateBereikDao,     Spring.Northwind.Dao.NHibernate">
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>

您还必须将对象引用添加到 Service.xml 并将其绑定(bind)到 BereikDao,如下所示:

<object id="BereikService" type="Spring.Northwind.Service.BereikService, Spring.Northwind.Service">
<property name="BereikDao" ref="BereikDao"/>
</object>

关于c# - Spring.net Nhibernate dao 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14768368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com