Fluent NHibernate: how can I use Int64 as an identifier?

I am completely new to the whole concept of ORM, NHibernate, and FluentNH, and I am trying to accomplish something that seems so simple ...

I am trying to get an object that has a field defined as Int64 in it. This field will be the identifier defined in the map file.

When trying to retrieve a record from the database, NHibernate returns the following error message: "An identifier of the wrong type was provided. Expected: System.Int32, received System.Int64"

I have a very simple object:

public class Holiday { public virtual Int64 HolidayID { get; set; } public virtual string Name { get; set; } public virtual int Day { get; set; } public virtual int Month { get; set; } public virtual bool IsActive { get; set; } public virtual HolidayGroup Group { get; set; } public Holiday() { } } 

The mapping file (for FluentNH) is as follows:

 public class HolidayMap : ClassMap<Holiday> { public HolidayMap() { Id(x => x.HolidayID); Map(x => x.Name); Map(x => x.Day); Map(x => x.Month); Map(x => x.IsActive); HasOne(x => x.Group); } } 

the structure of the table (generated by NH) is as follows:

 CREATE TABLE [dbo].[Holiday]( [HolidayID] [bigint] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](255) NULL, [Day] [int] NULL, [Month] [int] NULL, [IsActive] [bit] NULL, [HolidayGroup_id] [int] NULL, PRIMARY KEY CLUSTERED ( [HolidayID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] 

and finally, I'm trying to fetch a Holiday instance using this:

  public static Holiday Get(Int64 _holidayID) { Holiday _holiday = new Holiday(); try { using (var session = Global.NHibernate_SessionFactory.OpenSession()) { _holiday = session.Get<Holiday>(_holidayID); // EXCEPTION OCCURS HERE } } catch (Exception _ex) { // TODO : Implement proper logging } return _holiday; } 

What am I doing wrong? What is missing? When deleting a table and redefining my object using Int32 for ID, everything works! I need to use a larger type!

Many thanks!

EDIT 1: As Aaronaught is mentioned, I agree, my need for Int64 is a bit of an overhead for storing holidays ... But for a second, let me forget the Holidays concept. What will I do with my logging table (from 5 to 10 events (rows) per second!). Thanks!

EDIT 2: @Paco: I am using NHibernate 2.1.2.4000 and FluentNH 1.0.0.614

EDIT 3: After reusing Daniel Schilling's solution, rebuilt the (new) simple Holiday object that used Int64 as an identifier. I managed to get a record from the database successfully. As soon as the relation to the Group object was added , when I started the Holiday object, I got the same error message as before ... Here is the HolidayGroup class and map, if you can tell me what I did wrong ...

 public class HolidayGroup { public virtual int HolidayGroupID { get; set;} public virtual string Name { get; set; } public virtual string Notes { get; set; } public virtual bool IsActive { get; set; } public virtual IList<Holiday> Holidays { get; set; } public HolidayGroup() { Holidays = new List<Holiday>(); } } 

public HolidayGroupMap () {Id (x => x.HolidayGroupID); Map (x => x.Name); Map (x => x.Notes); Map (x => x.IsActive); HasMany (x => x.Holidays) .Cascade.All ()}

+4
source share
2 answers

I know that it’s not very useful to just say that it works for me, but ... it works for me. I use long for my id all the time with Fluent NHibernate. My versions:

  • NHibernate 2.1.0.4000
  • Fluent NHibernate 1.0.0.598

As others commented, it is possible that this could be a problem with NHibernate or Fluent NHibernate. To find out what the problem is, use the ExportTo method to find out what * .hbm.xml Fluent NHibernate generates. See the Export Mappings section at https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-configuration .

If FluentNHibernate is working correctly, you should see something like ...

 <id name="HolidayID" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="HolidayID" /> <generator class="identity" /> </id> 

... in the * .Holiday.hbm.xml file. If you see that System.Int64 , then the problem should be in NHibernate or in your code. If the problem is related to NHibernate - this should be the problem introduced with 2.1.0 (since it works for me). Try using the same version that I am using. If the problem goes away, then this was the new version of NHibernate.

If this does not fix the problem, then (unfortunately) the problem most likely lies in your own code. How is Global.NHibernate_SessionFactory created? Is it possible that one Configuration object is used to export the schema and another instance creates a SessionFactory ?

These are all the ideas that I have. Good luck.

EDIT 1: Regarding the attitude towards HolidayGroup (sorry - I haven’t noticed this before): I think you mean References instead of HasOne - see question 1622007 . In addition, you need to note one side of the Inverse() relationship - as a rule, you put this on the HasMany side.

EDIT 2: An incorrectly applied HasOne is certainly the source of this error. Individual relationships are mapped by default to two tables that share primary keys. In the primary key column for children, there will usually be a ratio of the foreign key to the primary primary key. Saying "Holiday-one-HolidayGroup", you set Holiday as the parent and HolidayGroup as a child (which is the opposite of what you wanted).

When NHibernate tries to download a HolidayGroup-related Holiday, it will receive the Holiday32 Group Int32 identifier and try to get the Holiday with the same identifier. However, Holiday has an Int64 ID, therefore, an error.

Make changes to "EDIT 1" and this problem should disappear.

  • Change HasOne to References .
  • Add Inverse() to HolidayGroup HasMany .
+2
source

Have you tried this?

 public class HolidayMap : ClassMap<Holiday> { public HolidayMap() { Id(x => x.HolidayID).SetAttribute("type", "Int64"); Map(x => x.Name); Map(x => x.Day); Map(x => x.Month); Map(x => x.IsActive); HasOne(x => x.Group); } } 
0
source

Source: https://habr.com/ru/post/1299486/


All Articles