[英]NHibernate map by code, no persister for:
因此,我已经为此苦苦挣扎了大约半天了。我要使用eclipselink和java,对于一个项目,我正在使用NHibernate和c#编写一些代码。 不幸的是,我猜缺少关于NHibernate上通过代码映射的文档。 我找不到我得到的错误的任何信息。
为了进行测试,我正在使用Northwind数据库,因此我创建了一个Customer类
public class Customer{
public virtual string CustomerID { get; set; }
public virtual string CompanyName { get; set; }
public virtual string ContactName { get; set; }
public virtual string ContactTitle { get; set; }
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string Region { get; set; }
public virtual string PostalCode { get; set; }
public virtual string Country { get; set; }
public virtual string Phone { get; set; }
public virtual string Fax { get; set; }
}
也是一个映射类:
public class CustomerMap: ClassMapping<Customer>
{
public CustomerMap() {
Schema("dbo");
Lazy(true);
Id(x => x.CustomerID, map => map.Generator(Generators.Assigned));
Property(x => x.CompanyName, map => map.NotNullable(true));
Property(x => x.ContactName);
Property(x => x.ContactTitle);
Property(x => x.Address);
Property(x => x.City);
Property(x => x.Region);
Property(x => x.PostalCode);
Property(x => x.Country);
Property(x => x.Phone);
Property(x => x.Fax);
}
}
但是我得到了没有持久性的错误
ModelMapper mm = new ModelMapper();
mm.AddMapping<CustomerMap>();
HbmMapping hbmm = mm.CompileMappingForAllExplicitlyAddedEntities();
XmlSerializer xml = new XmlSerializer(hbmm.GetType());
xml.Serialize(Console.Out, hbmm);
这是上面的代码生成的xml
<?xml version="1.0" encoding="Windows-1252"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="NHibernate.Classes.Domain" assembly="NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns="urn:nhibernate-mapping-2.2">
<class name="Customer" schema="dbo">
<id name="CustomerID" type="String">
<generator class="assigned" />
</id>
<property name="CompanyName" not-null="true" />
<property name="ContactName" />
<property name="ContactTitle" />
<property name="Address" />
<property name="City" />
<property name="Region" />
<property name="PostalCode" />
<property name="Country" />
<property name="Phone" />
<property name="Fax" />
</class>
</hibernate-mapping>
由于我正在通过代码来处理地图,因此我不想自己创建该xml,所以我不知道自己可能做错了什么。 我在Google各处搜索。
这是我打电话给地图实体的电话
_mapper.AddMappings(MappingAssembly.GetExportedTypes());
Configure.AddDeserializedMapping(_mapper.CompileMappingForAllExplicitlyAddedEntities(), "MyWholeDomain");
_mapper是ModelMapper。 我还明确添加了一个配置设置,以告诉应用程序我的映射在哪里。 所以我加载程序集,然后调用GetExportedTypes()
这是我整个的NhibernateInitializer类
public abstract class NHibernateInitializer : IDomainMapper
{
protected Configuration Configure;
private ISessionFactory _sessionFactory;
private readonly ModelMapper _mapper = new ModelMapper();
private Assembly _mappingAssembly;
private readonly String _mappingAssemblyName;
private readonly String _connectionString;
protected NHibernateInitializer(String connectionString, String mappingAssemblyName)
{
if (String.IsNullOrWhiteSpace(connectionString))
throw new ArgumentNullException("connectionString", "connectionString is empty.");
if (String.IsNullOrWhiteSpace(mappingAssemblyName))
throw new ArgumentNullException("mappingAssemblyName", "mappingAssemblyName is empty.");
_mappingAssemblyName = mappingAssemblyName;
_connectionString = connectionString;
}
public ISessionFactory SessionFactory
{
get
{
return _sessionFactory ?? (_sessionFactory = Configure.BuildSessionFactory());
}
}
private Assembly MappingAssembly
{
get
{
return _mappingAssembly ?? (_mappingAssembly = Assembly.Load(_mappingAssemblyName));
}
}
public void Initialize()
{
Configure = new Configuration();
Configure.EventListeners.PreInsertEventListeners = new IPreInsertEventListener[] { new EventListener() };
Configure.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] { new EventListener() };
Configure.SessionFactoryName(System.Configuration.ConfigurationManager.AppSettings["SessionFactoryName"]);
Configure.DataBaseIntegration(db =>
{
db.Dialect<MsSql2008Dialect>();
db.Driver<SqlClientDriver>();
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
db.IsolationLevel = IsolationLevel.ReadCommitted;
db.ConnectionString = _connectionString;
db.BatchSize = 20;
db.Timeout = 10;
db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
});
Configure.SessionFactory().GenerateStatistics();
Map();
}
private void Map()
{
_mapper.AddMappings(MappingAssembly.GetExportedTypes());
Configure.AddDeserializedMapping(_mapper.CompileMappingForAllExplicitlyAddedEntities(), "MyWholeDomain");
}
public HbmMapping HbmMapping
{
get { return _mapper.CompileMappingFor(MappingAssembly.GetExportedTypes()); }
}
public IList<HbmMapping> HbmMappings
{
get { return _mapper.CompileMappingForEach(MappingAssembly.GetExportedTypes()).ToList(); }
}
/// <summary>
/// Gets the domain entities.
/// </summary>
/// <returns></returns>
/// <remarks>by default anything that derives from EntityBase and isn't abstract or generic</remarks>
protected virtual IEnumerable<System.Type> GetDomainEntities()
{
List<System.Type> domainEntities = (from t in MappingAssembly.GetExportedTypes()
where typeof(EntityBase<Guid>).IsAssignableFrom(t)
&& (!t.IsGenericType || !t.IsAbstract)
select t
).ToList();
return domainEntities;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.