I've been trying all day to get one of my object to be saved with versions but to no avail. Please point out what I'm doing wrong, as I've tried SaveOrUpdate, Merge() and Update() after a Clear() call.
The business object:
public class MappedTest
{
public virtual Guid TestID { get; set; }
public virtual int VersionID { get; set; }
public virtual byte[] Content { get; set;}
public virtual DateTime DateSaved { get; set; }
}
The mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping ...>
<class name="TestImp.Definition.MappedTest, PythonTest" table="Tests">
<id name="TestID" unsaved-value="00000000-0000-0000-0000-000000000000">
<generator class="guid"/>
</id>
<version name="VersionID" column="VersionID" />
<property name="Content" column="TestObject" type="BinaryBlob"/>
<property name="DateSaved" column="Date"/>
`
The actual code:
using (var session = new Configuration().Configure().BuildSessionFactory().OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
if(session.Get<MappedTest>(mappedTest.TestID) == null)
{
session.Save(mappedTest);
}
else
{
session.Clear();
session.Update(mappedTest);
}
transaction.Commit();
}
}`
Thanks.
For insert try just with:
using (var session = new Configuration().Configure().BuildSessionFactory().OpenSession())
{
MappedTest mappedTest =new MappedTest();
using (ITransaction transaction = session.BeginTransaction())
{
session.SaveOrUpdate(mappedTest);
transaction.Commit();
}
}
for update:
using (var session = new Configuration().Configure().BuildSessionFactory().OpenSession())
{
MappedTest mappedTest =session.Get<MappedTest>(..an Id..);
mappedTest.YourProperty="newValue";
using (ITransaction transaction = session.BeginTransaction())
{
session.SaveOrUpdate(mappedTest);
transaction.Commit();
}
}
If you need it try use a session.Flush() to force database operations.
Another possibility that took me some time to realize and is not covered yet on this thread: In such case, check if your mapper is set on ReadOnly. NHibernate does not tell anything when asked to save or update with a ReadOnly mapper.
Related
I want to use mapping by code so I have a class Employee (namespace NHibernateTests.Classes) and a class EmployeeMappings (namespace NHibernateTests.Mappings)
My whole nhibernate configuration is set in an xml file hibernate.cfg.xml which currently goes like this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">User Id=NHIBERNATE;Password=NHIBERNATE;Data Source=XE</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<mapping assembly="NHibernateTests"/>
</session-factory>
</hibernate-configuration>
Which gives me the runtime error : No persister for: NHibernateTests.Classes.Employee
I tried (and error) some setting for mapping element but with no luck. I read how to set ressource for hbm.xml elements but couldn't find an answer for by code mapping.
namespace NHibernateTests.Classes
{
public class Employee
{
public virtual Address Address { get; set; }
public virtual string FirstName { get; set; }
public virtual int Id { get; set; }
}
}
namespace NHibernateTests.Mappings
{
public class EmployeeMappings : ClassMapping<Employee>
{
public EmployeeMappings()
{
this.Id(e => e.Id, mapper =>
{
mapper.Generator(Generators.HighLow);
});
}
}
}
With a mapping by code you should configure also your factory by code. There is one of few how-to:
NHibernate 3.2 Mapping by Code – Basic Mapping
cited code snippets (see above link for more details)
private static Configuration ConfigureNHibernate()
{
var configure = new Configuration();
configure.SessionFactoryName("BuildIt");
configure.DataBaseIntegration(db =>
{
db.Dialect();
db.Driver();
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
db.IsolationLevel = IsolationLevel.ReadCommitted;
db.ConnectionStringName = "NH3";
db.Timeout = 10;
// enabled for testing
db.LogFormattedSql = true;
db.LogSqlInConsole = true;
db.AutoCommentSql = true;
});
var mapping = GetMappings();
configure.AddDeserializedMapping(mapping, "NHSchemaTest");
SchemaMetadataUpdater.QuoteTableAndColumns(configure);
return configure;
}
thew way how to get HbmMapping
private static HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetAssembly(typeof(ProvinceMap)).GetExportedTypes());
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mapping;
}
I wanted to test whether my entities can be persisted to the database or not, so I came across this article:
http://www.codethinked.com/nhibernate-20-sqlite-and-in-memory-databases
My code to initialize the session factory is the same the one in the article:
public class NHibernateInMemoryTestFixtureBase
{
protected static ISessionFactory sessionFactory;
protected static Configuration configuration;
public static void InitalizeSessionFactory(params Assembly[] assemblies)
{
if (sessionFactory != null)
return;
var properties = new Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SQLite20Driver");
properties.Add("dialect", "NHibernate.Dialect.SQLiteDialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("connection.connection_string", "Data Source=:memory:;Version=3;New=True;");
properties.Add("connection.release_mode", "on_close");
configuration = new Configuration();
configuration.Properties = properties;
foreach (Assembly assembly in assemblies)
{
configuration = configuration.AddAssembly(assembly);
}
sessionFactory = configuration.BuildSessionFactory();
}
public ISession CreateSession()
{
ISession openSession = sessionFactory.OpenSession();
IDbConnection connection = openSession.Connection;
new SchemaExport(configuration).Execute(false, true, false, true, connection, null);
return openSession;
}
}
And here's my test:
[Test]
public void IWillChangeThisNameLater()
{
InitalizeSessionFactory(typeof(LogRepository).Assembly);
var session = this.CreateSession();
Log log = Log.New("a", "b", "I");
session.Save(log);
session.Flush();
Assert.Greater(log.IDColumn, 0);
}
And the problem is, I removed the "a" property of Log from the log.hbm.xml and session.Save(log) is not throwing an exception or anything, it just works...
This must be obvious and on porpose, but I fail to find out why that is, how can it save it if is not mapped, is that how the in memory database work? how can I test my mapping then?
I mainly did this in-memory test so that I can know right away if a valid entity is failing to persist, of course that would include missing properties on the mapping file.
Any thoughts will be appreciated.
EDIT:
As requested,
the Log entity definition:
public class Log : DomainBase<Log, ILogRepository<Log>>
{
private int logId;
private string tableName;
private string field;
private string userLogin;
protected Log()
{
}
protected Log(string tableName, string field, string userLogin)
{
TableName = tableName;
Field = field;
UserLogin = userLogin;
}
public virtual int LogId { get; set; }
public virtual string TableName { get; set; }
public virtual string Field { get; set; }
public virtual string UserLogin { get; set; }
}
the Log Mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DomainProject" table="Log" lazy="true">
<id name="logId" column="ID" type="int">
<generator class="native" />
</id>
<property name="TableName" column="TableName" type="string" />
<property name="Field" column="Field" type="string" />
<property name="UserLogin" column="UserLogin" type="string" />
</class>
</hibernate-mapping>
If a class contains a property that is not mentioned in the mappings, NHibernate will ignore the property.
I'm using NHibernate on a legacy database with Oracle 8i client. I can perform Get and Delete on the database table, but can't save or update entries. The exception is as follow, the sqlString consists of question marks and I don't know why.
Nhibernate.Exceptions.GenericADOException:
{"could not update: [MIAP.Domain.Entities.MITFORG#3][SQL: UPDATE MITFORG SET TREELEVEL = ?, PARTENTID = ?, FORGNAME = ?, FORGINFO = ?, ACTIVE = ?, MUTATOR = ?, INPDATETIME = ?, UPDDATETIME = ? WHERE FORGID = ?]"}
Here are my entity class and mapping:
public class MITFORG {
private long fORGID;
private long? tREELEVEL;
private long? pARTENTID;
private string fORGNAME;
private string fORGINFO;
private string aCTIVE;
private long? mUTATOR;
private DateTime? iNPDATETIME;
private DateTime? uPDDATETIME;
public MITFORG() { }
public virtual long FORGID {
get {
return this.fORGID;
}
set {
this.fORGID = value;
}
}
public virtual long? TREELEVEL
{
get {
return this.tREELEVEL;
}
set {
this.tREELEVEL = value;
}
}
public virtual long? PARTENTID
{
get {
return this.pARTENTID;
}
set {
this.pARTENTID = value;
}
}
public virtual string FORGNAME {
get {
return this.fORGNAME;
}
set {
this.fORGNAME = value;
}
}
public virtual string FORGINFO {
get {
return this.fORGINFO;
}
set {
this.fORGINFO = value;
}
}
public virtual string ACTIVE {
get {
return this.aCTIVE;
}
set {
this.aCTIVE = value;
}
}
public virtual long? MUTATOR
{
get {
return this.mUTATOR;
}
set {
this.mUTATOR = value;
}
}
public virtual DateTime? INPDATETIME
{
get {
return this.iNPDATETIME;
}
set {
this.iNPDATETIME = value;
}
}
public virtual DateTime? UPDDATETIME
{
get {
return this.uPDDATETIME;
}
set {
this.uPDDATETIME = value;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="MIAP.Domain" namespace="MIAP.Domain.Entities" xmlns="urn:nhibernate-mapping-2.2">
<class name="MITFORG" table="MITFORG" lazy="true" >
<id name="FORGID">
<generator class="assigned" />
</id>
<property name="TREELEVEL"></property>
<property name="PARTENTID"></property>
<property name="FORGNAME"></property>
<property name="FORGINFO"></property>
<property name="ACTIVE"></property>
<property name="MUTATOR"></property>
<property name="INPDATETIME"></property>
<property name="UPDDATETIME"></property>
</class>
</hibernate-mapping>
I've checked property names and table column names. Since the FORGID is assigned by the application, I changed the generator class to "assigned". It doesn't work with "identity" either. Could someone please point me the direction to debug this?
Edited: Code to save entries
Dim mitforgRepository As New MITFORGRepository
Dim mitforg As MITFORG = mitforgRepository.GetById(3)
mitforg.FORGINFO = "T"
mitforg.ACTIVE = "Y"
mitforg.FORGINFO = "T"
mitforg.INPDATETIME = Now
mitforg.MUTATOR = 324
mitforg.PARTENTID = 335
mitforg.TREELEVEL = 1
mitforg.UPDDATETIME = Now
mitforgRepository .Save(mitforg)
And here is the Repository class:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using MIAP.Domain.Entities;
namespace MIAP.Domain.Repositories
{
public class MITFORGRepository : IRepository<MITFORG, Int64?>
{
private static ISession GetSession()
{
return SessionProvider.SessionFactory.OpenSession();
}
public MITFORG GetById(Int64? id)
{
using (ISession session = GetSession())
{
return session.Get<MITFORG>(id);
}
}
public void Save(MITFORG saveObj)
{
using (ISession session = GetSession())
{
using (ITransaction trans = session.BeginTransaction())
{
session.SaveOrUpdate(saveObj);
trans.Commit();
}
}
}
public void Delete(MITFORG delObj)
{
using (ISession session = GetSession())
{
using (ITransaction trans = session.BeginTransaction())
{
session.Delete(delObj);
trans.Commit();
}
}
}
}
}
The InnerException is System.Data.OracleClient.OracleException, ORA-12571
And here's the stack trace:
於 System.Data.OracleClient.OracleConnection.CheckError(OciErrorHandle errorHandle, Int32 rc)
於 System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle, CommandBehavior behavior, Boolean needRowid, OciRowidDescriptor& rowidDescriptor, ArrayList& resultParameterOrdinals)
於 System.Data.OracleClient.OracleCommand.ExecuteNonQueryInternal(Boolean needRowid, OciRowidDescriptor& rowidDescriptor)
於 System.Data.OracleClient.OracleCommand.ExecuteNonQuery()
於 NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd) 於 c:\Users\oskar.berggren\Documents\Projects\nhibernate-core-3\src\NHibernate\AdoNet\AbstractBatcher.cs: 行 203
於 NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation) 於 c:\Users\oskar.berggren\Documents\Projects\nhibernate-core-3\src\NHibernate\AdoNet\NonBatchingBatcher.cs: 行 40
於 NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) 於 c:\Users\oskar.berggren\Documents\Projects\nhibernate-core-3\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs: 行 2799
If you're using assigned id's you can't use SaveOrUpdate as NHibernate won't know whether it's a new instance (ie. to Save/do an insert) or an existing instance(ie. to Update).
You need to then be explicit about whether you're inserting a new entity (session.Save) or updating an existing entity (session.Update).
From the looks of it, you want to create a new object and save it into the database but what you are doing is loading the object using the Nhibernate Session and then updating the properties of it.. what this tells the Nhibernate session is that you have an object associated in the db with the given id and now you want to update certain properties when infact you want an insert statement to run.
Thus the right way is to create a new MitForg object and then call Session.SaveOrUpdate() on it and Nhibernate should take care of the insertion thereafter.
You could also try using Session.Merge().
Let me know if this works out for you..
Turns out it's a Unicode to ASCII conversion problem. I followed the solution in the links below. What's needed is adding a type attribute to all string type properties in the mapping file like this:
<property name="FORGNAME" type="AnsiString" ></property>
Recently my colleague also encountered some kind of Unicode/ASCII conversion problem but I never thought it would have been the answer. The exception message is just misleading...
Thank Martin for the inspiring suggestion!
NHibernate and The Case of the Crappy Oracle
NHibernate and ORA-12571 Errors
I'm following the NHibernate getting started tutorial: "Your first NHibernate based application". I'm at the point where I create a Product object then use Session.get() to prove I can read the object.
It works fine with SQL Server Ce, but I'm getting an exception when I try to use DB2. (The SQL Server Ce version works - that is. There are some minor changes between the versions like int instead of GUID for the Id.)
I'm very experienced with Hibernate and SQL databases. This is my first experience with NHibernate and DB2. (Coming from the Java world). I'd appreciate any suggestions, especially from the (evidently few) people who are using NHibernate on DB2.
Rob
The full exception is
Test method Examples.DB2.NHibernateExamples.Can_add_new_product threw
exception: NHibernate.Exceptions.GenericADOException: could not load
an entity: [Examples.DB2.Domain.Product#1][SQL: SELECT product0_.Id as
Id1_0_, product0_.Name as Name1_0_, product0_.Category as
Category1_0_, product0_.Discontinued as Disconti4_1_0_ FROM Product
product0_ WHERE product0_.Id=?] ---> System.IndexOutOfRangeException:
Invalid index 0 for this DB2ParameterCollection with Count=0.
This is happening in the Get(...) call in the following code:
[TestInitialize]
public void TestInitialize()
{
TestFixtureSetup();
SetupContext();
}
[TestMethod]
public void Can_add_new_product()
{
var product = new Product { Id = 1, Name = "Apple", Category = "Fruits"};
using (ISession session = _sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(product);
transaction.Commit();
}
}
using (ISession session = _sessionFactory.OpenSession())
{
//var query = session.CreateQuery("from Product");
//var products = query.List<Product>();
//Assert.AreEqual(1, products.Count);
var fromDb = session.Get<Product>(product.Id);
Assert.IsNotNull(fromDb);
Assert.AreNotSame(product, fromDb);
Assert.AreEqual(product.Name, fromDb.Name);
Assert.AreEqual(product.Category, fromDb.Category);
}
}
private void TestFixtureSetup()
{
_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly(typeof (Domain.Product).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();
}
private void SetupContext()
{
new SchemaExport(_configuration).Execute(true, true, false);
}
The exception seems to indicate that the Id paramter isn't being passed to the DB2 query. If I uncomment the three lines before the Get(), it works fine since the row is already cached and the Get() doesn't actually go to the database.
Here is the definition of Product.cs:
using System;
namespace Examples.DB2.Domain
{
class Product
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
}
}
Here is Product.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Examples.DB2"
namespace="Examples.DB2.Domain">
<class name="Product">
<id name="Id">
<generator class="native" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" type="YesNo"/>
</class>
</hibernate-mapping>
Here is hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.DB2Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.DB2Driver</property>
<property name="connection.connection_string">Database=SAMPLE; UID=DEV; PWD=password</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
I'm working in Visual Studio 2010 Premium on Windows 7. I'm using DB2 Express-C 9.7.4.
Ok,
I found the answer ... not exactly sure of the solution yet, but in the final release of NHibernate they added a call to AdoNet\AbstractBatcher.cs called RemoveUnusedCommandParameters. This procedure calls Driver.RemoveUnusedCommandParameters.
My solution for now is just to comment out this call and let the function do nothing.
I will bring this up with the nhusers group and see if there is a better long term solution.
thanks
dbl
I have the current scenario whereby an Article has only 1 Outcome each. Each Article may or may not have an Outcome.
In theory, this is a one-to-one mapping, but since NHibernate does not really support one-to-one, I used a One-To-Many to substitute. My Primary Key on the child table is actually the ArticleID (FK).
So I have the following setup:
Classes
public class Article
{
public virtual Int32 ID { get;set;}
private ICollection<ArticleOutcome> _Outcomes {get;set;}
public virtual ArticleOutcome Outcome
{
get {
if( this._Outcomes !=null && this._Outcomes.Count > 0 )
return this._Outcomes.First();
return null;
}
set {
if( value == null ) {
if( this._Outcomes !=null && this._Outcomes.Count > 0 )
this._Outcomes.Clear();
}
else {
if( this._Outcomes == null )
this._Outcomes = new HashSet<ArticleOutcome>();
else if ( this._Outcomes.Count >= 1 )
this._Outcomes.Clear();
this._Outcomes.Add( value );
}
}
}
}
public class ArticleOutcome
{
public virtual Int32 ID { get;set; }
public virtual Article ParentArticle { get;set;}
}
Mappings
public class ArticleMap : ClassMap<Article>
{
public ArticleMap() {
this.Id( x=> x.ID ).GeneratedBy.Identity();
this.HasMany<ArticleOutcome>( Reveal.Property<Article>("_Outcomes") )
.AsSet().KeyColumn("ArticleID")
.Cascade.AllDeleteOrphan() //Cascade.All() doesn't work too.
.LazyLoad()
.Fetch.Select();
}
}
public class ArticleOutcomeMap : ClassMap<ArticleOutcome>
{
public ArticleOutcomeMap(){
this.Id( x=> x.ID, "ArticleID").GeneratedBy.Foreign("ParentArticle");
this.HasOne( x=> x.ParentArticle ).Constrained ();
//This do not work also.
//this.References( x=> x.ParentArticle, "ArticleID" ).Not.Nullable();
}
}
Now my problem is this:
It works when I do an insert/update of the Outcome.
e.g.
var article = new Article();
article.Outcome = new ArticleOutcome { xxx = "something" };
session.Save( article );
However, I encounter SQL errors when attempting to delete via the Article itself.
var article = session.Get( 123 );
session.Delete( article ); //throws SQL error.
The error is something to the like of Cannot insert NULL into ArticleID in ArticleOutcome table.
The deletion works if I place Inverse() to the Article's HasMany() mapping, but insertion will fail.
Does anyone have a solution for this? Or do I really have to add a surrogate key to the ArticleOutcome table?
Solution
Here's the mapping for Fluent if anyone is interested.
public class ArticleMap : ClassMap<Article>
{
public ArticleMap() {
this.Id( x=> x.ID ).GeneratedBy.Identity();
this.HasOne( x=> x.Outcome ).Cascade.All();
}
}
public class Article
{
//... other properties
public virtual ArticleOutcome Outcome { get;set;}
}
NHibernate does support one-to-one.
I have a case very similar to yours. Here are the relevant parts:
class Articule
{
public virtual ArticleOutcome Outcome { get; set; }
}
class ArticuleOutcome
{
public virtual Article ParentArticle { get; set; }
}
Mapping (sorry, I don't use Fluent, but it shouldn't be hard to translate):
<class name="Article">
<id name="ID">
<generator class="identity"/>
</id>
<one-to-one name="Outcome" cascade="all"/>
</class/>
<class name="ArticleOutcome">
<id name="ID" column="ArticleId">
<generator class="foreign">
<param name="property">ParentArticle</param>
</generator>
</id>
<one-to-one name="ParentArticle" constrained="true"/>
</class/>
Usage:
var article = new Article();
article.Outcome = new ArticleOutcome
{
xxx = "something",
ParentArticle = article
};
session.Save(article);
Delete should work as you are using it now.