I'm trying to persist a SortedList<DateTime, double?> with NHibernate 2.0. When an item's value (not key) is null, that item is not persisted to the database.
Class snippet:
public class TimeSeries{
...
public TimeSeries(){
Data = new SortedList<DateTime, double?>();
}
public virtual IDictionary<DateTime, double?> Data { get; private set; }
...
}
Mapping snippet:
...
<map name="Data" cascade="all" lazy="true" sort="natural" collection-type="sorted-list">
<key column="ID"/>
<index type="Date" column="Period"/>
<element type="double" column="Value" not-null="false"/>
</map>
...
Usage snippet:
var series = new TimeSeries();
series.Data.Add(new DateTime(2000, 1, 1), 1);
series.Data.Add(new DateTime(2000, 1, 2), null);
series.Data.Add(new DateTime(2000, 1, 3), 3);
repository.Add(series);
The first and third items are persisted, but not the second. If I change the double value from a null value to a non-null value, it saves fine.
Any idea how I can save the null values? I'm using MySql and the database schema allows for nulls.
Thanks,
Marcus
Related
Given are tables Item and ItemTranslation where the latter has a NOT-Nullable foreign key on Item.
ItemTranslation.hbm.xml only has its properties Text and LanguageCode, it does NOT map Item.
Item.hbm.xml:
<bag name="Translations" cascade="all-delete-orphan">
<key column="ItemID" />
<one-to-many class="ItemTranslation, SomeNamespace" />
</bag>
Now when I do the following:
Item item = new Item();
item.Translations.Add( new ItemTranslation { LanguageCode = "DE", Text = "Test DE" } );
item.Translations.Add( new ItemTranslation { LanguageCode = "EN", Text = "Test EN" } );
item.Save();
NHibernate throws the following exception:
System.Data.SqlClient.SqlException : Cannot insert the value NULL into column 'ItemID', table 'someDb.dbo.ItemTranslation'; column does not allow nulls. INSERT fails.
Cannot insert the value NULL into column 'ItemID', table 'someDb.dbo.ItemTranslation'; column does not allow nulls.
I could fix it either by mapping ItemID as many-to-one in IssuedItemTranslation.hbm.xml or making the column NULLable.
But both ways are kind of ugly.
Is there any other possibility? Maybe with some change in the bag-mapping?
Thank you in advance.
If we do not want to use inverse mapping (see below) .. we must left the column in DB nullable.
That is how it works. NHibernate will
insert children (or parent)
insert parent (or children) read more here 9.6. Flush
update children with parent id
Another way: Inverse mapping
There is nothing bad on explicit Item back reference mapping on ItemTranslatin. I do that always. And If we really do not like it.. it could be protected property.
But then, we would need inverse mapping.
<bag name="Translations" cascade="all-delete-orphan" inverse="true">
And also set the reference on both sides.
Item item = new Item();
var tr1 = new ItemTranslation {
LanguageCode = "DE",
Text = "Test DE"
Item = item } ;
item.Translations.Add(tr1);
...
This solution does not require item column to be nullable...
That would be the way I suggest. Read more here:
Minimal and correct way to map one-to-many with NHibernate
It works when mapping the bag as follows:
<bag name="Translations" cascade="all-delete-orphan">
<key column="ItemID" not-null="true" update="false" />
<one-to-many class="ItemTranslation, SomeNamespace" />
</bag>
Is this the correct solution?
I'm new to NHibernate (using latest version) and I am having an issue mapping an object in our generic dataloading application to a database.
The database is third party so we cant make changes there.
Our object is:
public class GenericObjectValue
{
public string ObjectId { get; set; }
public string ObjectTypeId { get; set; }
public string measurementId { get; set; }
public DateTime timestamp { get; set; }
public Double Value { get; set; }
}
Datasource tables we use:
Table t_data_point
(
id (PK, int, not null)
object_id (FK, Varchar(30), not null)
object_type_id (FK, Varchar(30), not null);
measurement_id (FK, Varchar(30), not null);
)
Table t_data_point_Value
(
data_point_id (PK, FK, int, not null)
timestamp (PK, FK, datetime, not null)
version (PK, FK, int, not null)
value (numeric(18,6), not null);
)
The mapping i have configured is:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Phoenix.Model" assembly="Phoenix.Common">
<class name="MeasValue" table="t_data_point_Value">
<id column="data_point_id" type="int" />
<property name="Timestamp" column="timestamp "/>
<property name="Value" column="value"/>
<join table="t_data_point">
<key column="id" />
<property name="measurementId" column="measurement_id" />
<property name="ObjectId" column="object_id" />
<property name="ObjectTypeId" column="object_type" />
</join>
</class>
</hibernate-mapping>
Not sure if i'm doing something stupid or something that just isn't possible but when i run the code for this i get back the correct number of results but the results are just duplications of the first result returned i.e. timestamp and value are the same.
If you need more information please let me know.
I've seen this when your id is mapped incorrectly, ie to a column that is not unique. Nhibernate will reuse the 1st object it finds in cache with the same id.
There is a free mapping tool that could help point you in the right direction. It wont create the join but it will create the basic columns and id http://nmg.codeplex.com.
I would like to use "hilo" generator but there is no any complete example how to create "specific" table as NH documentation says, and which values pass to it.
The following code fragments taken from NH tutorial
public class Cat
{
private Int64 id;
private string name;
private char sex;
private float weight;
public Cat()
{}
public virtual Int64 Id
{
get { return id; }
set { id = value; }
}
....
}
Mapper
<hibernate-mapping ...>
<class name="Cat" table="Cat">
<id name="Id" >
<column name="CatId" sql-type="Int64" not-null="true"/>
<generator class="hilo"/>
</id>
<property name="Name">
<column name="Name" length="16" not-null="true" />
</property>
....
</class>
</hibernate-mapping>
DB table "Cat"
CatId bigint NOT NULL
Name varchar(16) NOT NULL
Sex char(1) NULL
Weight real NULL
doesn't create anything in the database by default.
Parameters in the "id" node
<param name="table">hi_value</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
gives "Invalid object name 'hi_value'" error message, without them I'm getting "Invalid object name 'hibernate_unique_key'."
Cuid.Comb that is shown in their tutorial works good but gives 99.12% of fragmentation when I added in a loop 20K cat objects.
Can somebody point me to an example of "hilo" implementation or give a tip what I'm missing?
Thanks.
This solution solved my problem. It's fairly simple, don't know why on nhibernate site there is no tiny example like that.
You may be running into NH-2687.
Using NHibernate I want to filter a collection in a class to contain ONLY a subset of possible objects. Below I am including a sample table data to help explain. I can find no way to do this using NHibernate.
Table:DataObject
DataObjectId(PK) / Name / CurrentVersion
11 "data.txt" 2
12 "info.txt" 3
Table:DataObjectVersion
Id / Comment / VersionNumber / DataObjectId(FK)
31 "Genesis" 1 11 <= Ignore this object
32 "Changed data" 2 11 <= Get this object
34 "Genesis" 1 12 <= Ignore this object
35 "Changed info" 2 12 <= Ignore this object
36 "Added info" 3 12 <= Get this object
I want to join on a non-foreign key DataObject.CurrentVersion = DataObjectVersion.VersionNumber for each DataObject in one command.
Here are the classes and mapping files:
public class DataObject
{
public virtual int DataObjectId { get; set; }
public virtual string Name { get; set; }
public virtual int CurrentVersionNumber { get; set; }
public virtual IList<DataObjectVersion> Versions { get; set; }
}
<class name="DataObject" table="DataObject" lazy="false">
<id name="DataObjectId" column="DataObjectId" type="int">
<generator class="assigned" />
</id>
<property name="Name" column="Name" type="String(512)" />
<property name="CurrentVersionNumber" column="CurrentVersionNumber" type="int" />
<bag name="Versions" cascade="all-delete-orphan" inverse="true" lazy="false" >
<key column="DataObjectId" />
<one-to-many class="DataObjectVersion" />
</bag>
</class>
public class DataObjectVersion
{
public virtual int DataObjectVersionId { get; set; }
public virtual string Comment { get; set; }
public virtual int VersionNumber { get; set; }
public virtual int DataObjectId { get; set; }
}
<class name="DataObjectVersion" table="DataObjectVersion" lazy="false">
<id name="Id" column="DataObjectVersionId" type="int">
<generator class="assigned" />
</id>
<property name="Comment" column="Comment" type="String(512)" />
<property name="VersionNumber" column="VersionNumber" type="int" />
<property name="DataObjectId" column="DataObjectId" type="int" />
</class>
if you want to filter the collection on demand, using a filter is a valid choice.
You would need to declare the filter on both the Version class and in the bag element and apply the filter from the NHibernateSession.EnableFilter method
if you always want to fetch a single Version in the bag then implement a 'where' in the mapping of the bag:
<bag name="Versions" cascade="all-delete-orphan" inverse="true" lazy="false" where="CurrentVersionNumber = Versions.VersionNumber" >
<key column="DataObjectId" />
<one-to-many class="DataObjectVersion" />
</bag>
note that in the 'where' you write proper SQL not HQL and as such the proper SQL i write above probably has to be changed to reflect your schema
Additionally if a single object is to be fetched setting up a bag and the according IList may be an overkill.
Applying a formula property and a DataObjectVersion object in the class may be more appropriate
in the class DataObject replace the IList with
public virtual DataObjectVersion Version { get; set; }
and in the mapping replace the 'bag' with something in the lines of
<property name="Version" type="DataObjectVersion" update="false" insert="false" formula="(select v.DataObjectVersionId, v.Comments, v.VersionNumber, v.DataObjectId from DataObjectVersion v where v.VersionNumber = CurrentVersionNumber)" />
again only proper SQL is allowed
i've used computed properties with native datatypes (datetime, string etc) and fetching an Entity may (or may not) need something more or different
Last but not least, you could apply a filter on the collection after you have fetched the primary object DataObject by creating a filter on the collection
IList<DataObjectVersion> fVersion =
NHibernateSession.CreateFilter(do.Versions, "where VersionNumber = :ver")
.SetParameter("ver", do.CurrentVersionNumber)
.List<DataObjectVersion>();
where the do.Versions collection is not initialized, only the results fetched in the separate fVersion collection and this is a second SELECT after already having made the round-trip to the db for the DataObject fetch.
Presumably your VersionNumber increments as the user changes the data and you're trying to get the latest one. If you consider the VersionNumber as an "Age" field instead (i.e. where 0 is the latest / youngest version, 1 is the next oldest and so on) then your problems becomes how to get all the entities with an Age of 0. This can be done using a filter: http://nhibernate.info/doc/nh/en/index.html#objectstate-filters
I have a class with following description:
public class Customer {
public ISet<Client> Contacts { get; protected set;}
}
I want to map Contacts property onto following table:
CREATE TABLE user_contacts (
user1 uuid NOT NULL,
user2 uuid NOT NULL
)
I want it to map bidirectionally, i.e. when Customer1 added to Customer2's Contacts, Customer1's Contacts collection should contain Customer2 (maybe only after entity reload). How could I do that?
Update Sure I can map left-to-right and right-to-left sets and then combine then at runtime, but it'll... hmm... untasty... Is there other solution? Any way, thank you very match, FryHard!
Take a look at this link on what hibernate calls unidirectional many-to-many associations. In Castle ActiveRecord I make use of HasAndBelongsToMany links, but I am not sure how exactly it is mapped in nhibernate.
Though taking a look at your question a little deeper, it looks like you will be linking bidirectionally from customer to user_contacts, which could break the many-many link. I will play with an example and see what I can come up with.
An Export of the hbm files from ActiveRecord shows this
<?xml version="1.0" encoding="utf-16"?>
<hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateMapping.Customer, NHibernateMapping" table="Customer" schema="dbo">
<id name="Id" access="property" column="Id" type="Int32" unsaved-value="0">
<generator class="identity">
</generator>
</id>
<property name="LastName" access="property" type="String">
<column name="LastName" not-null="true"/>
</property>
<bag name="ChildContacts" access="property" table="user_contacts" lazy="false">
<key column="user1" />
<many-to-many class="NHibernateMapping.Customer, NHibernateMapping" column="user2"/>
</bag>
<bag name="ParentContacts" access="property" table="user_contacts" lazy="false" inverse="true">
<key column="user2" />
<many-to-many class="NHibernateMapping.Customer, NHibernateMapping" column="user1"/>
</bag>
</class>
</hibernate-mapping>
ActiveRecord example:
[ActiveRecord("Customer", Schema = "dbo")]
public class Customer
{
[PrimaryKey(PrimaryKeyType.Identity, "Id", ColumnType = "Int32")]
public virtual int Id { get; set; }
[Property("LastName", ColumnType = "String", NotNull = true)]
public virtual string LastName { get; set; }
[HasAndBelongsToMany(typeof(Customer), Table = "user_contacts", ColumnKey = "user1", ColumnRef = "user2")]
public IList<Customer> ChildContacts { get; set; }
[HasAndBelongsToMany(typeof(Customer), Table = "user_contacts", ColumnKey = "user2", ColumnRef = "user1", Inverse = true)]
public IList<Customer> ParentContacts { get; set; }
}
Hope it helps!