-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Legacy DB Mapping Problem
PostPosted: Thu Sep 27, 2007 7:06 pm 
Newbie

Joined: Thu Sep 27, 2007 4:37 pm
Posts: 4
I have a DB which uses more than 1 table to map an inheritance structure.

The DB (MS SQL) looks like:

Code:
CREATE TABLE [dbo].[A](
   [xp] [int] NOT NULL,
   [xa] [datetime] NULL,
   [s] [varchar](50) NULL,

CREATE TABLE [dbo].[B](
   [xp] [int] NOT NULL,
   [xa] [datetime] NULL,
   [t] [varchar](50) NULL,


sadly, the columns xp and xa appear in both tables.

The mapping is:

Code:
<?xml version='1.0'?>
<hibernate-mapping auto-import="false" xmlns="urn:nhibernate-mapping-2.2">
   <class name="nhtest.A, nhtest" table="A">
      <id name="xp" column="xp" type="Int32">
         <generator class="assigned" />
      </id>
      <property name="xa" column="xa" type="DateTime" />
      <property name="s" column="s" type="String" />
   </class>
   <joined-subclass extends="nhtest.A, nhtest" name="nhtest.B, nhtest " table="B">
      <key column="xp" />
      <property name="xp" column="xp" type="Int32" />
      <property name="xa" column="xa" type="DateTime" />
      <property name="t" column="t" type="String" />
   </joined-subclass>
</hibernate-mapping>


and the classes are:

Code:
  class A
      {
      protected Int32? _xp = null;
      protected DateTime? _xa = null;
      protected string _s = null;

      public virtual Int32? xp
         {
         get {return _xp;}
         set {_xp = value;}
         }

      public virtual DateTime? xa
         {
         get {return _xa;}
         set {_xa = value;}
         }

      public virtual string s
         {
         get {return _s;}
         set {_s = value;}
         }

      }

   class B : A
      {
      protected new Int32? _xp = null;
      protected new DateTime? _xa = null;
      protected string _t = null;

      public virtual new Int32? xp
         {
         get {return _xp;}
         set {_xp = value;}
         }

      public virtual new DateTime? xa
         {
         get {return _xa;}
         set {_xa = value;}
         }

      public virtual string t
         {
         get {return _t;}
         set {_t = value;}
         }

      }


When I attempt to insert into an instance of "B":

Code:
         
B c = new B();
c.xp = 36;
((A)c).xp = 48;
session.Save(c);
session.Flush();


NHibernate generates:

Code:
NHibernate: INSERT INTO B (xp, xa, t, xp) VALUES (@p0, @p1, @p2, @p3); @p0 = '36
', @p1 = '', @p2 = '', @p3 = '36'


NHibernate has grabbed "36" for both instances of the member "xp", when I expected it to grab "36" for B.xp and 48 for A.xp. Additionally the generated SQL does not specifiy table names on the inserts, so it's ambiguous which "xp" column is to be inserted into.

It's unfortunate that I cannot change the DB structure; I must find a way to map it, as it is.

Is there a way to tell NHibernate that there is an xp in both table "A" and table "B" which maps to "xp" in class "A" and class "B"?

Thank-you, in advance, for any help you can offer.


Top
 Profile  
 
 Post subject: Legacy DB Mapping Problem
PostPosted: Fri Sep 28, 2007 7:17 am 
Beginner
Beginner

Joined: Wed Nov 29, 2006 10:32 am
Posts: 34
You should call the two *properties* differently; and then define that they both map to a column called xp in the mapping files.
I other words: If you dont (have to) use the new keyword, you should get it working.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.