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: Sharing a table for collection items.
PostPosted: Mon Oct 29, 2007 6:44 am 
Newbie

Joined: Mon Oct 29, 2007 6:13 am
Posts: 2
I'm trying to share a table of simple elements between to different objects, but getting a foreign key constraint added when I generate the schema that is causing errors.

Here's a summary of what I'm trying to do:

Object A has a collection of decimals
Object B has a collection of decimals
Both decimal collections point to a shared table.

However the schema generation adds foreign key constraints for tables A and B, so that whenever I insert something it clashes with the foreign key for the other object.

I've worked round it by using separate tables, but these are proliferating, so I'd quite like to be able to use a single table for all.

Here's a contrived example that runs as a console app to show what I mean:

Code:
public class FirstClass
    {
        public FirstClass() { }
        public Guid Id;
        public IList<decimal> Numbers = new List<decimal>();
    }

    public class SecondClass
    {
        public SecondClass() { }
        public Guid Id;
        public IList<decimal> Numbers = new List<decimal>();
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Create Schema
            NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
            cfg.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly());
            NHibernate.Tool.hbm2ddl.SchemaExport se = new NHibernate.Tool.hbm2ddl.SchemaExport(cfg);
            se.SetOutputFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "schema.sql"));
            se.Execute(true, true, false, true);

            //Save one of each class
            using (ISessionFactory sessionFactory = cfg.BuildSessionFactory())
            {
                using (ISession session = sessionFactory.OpenSession())
                {
                    FirstClass first = new FirstClass();
                    first.Numbers.Add(1.0M);

                    SecondClass second = new SecondClass();
                    second.Numbers.Add(2.0M);

                    session.Save(first);
                    session.Save(second);

                    try
                    {
                        session.Flush();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }

                }
            }

        }
    }

And mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHListTest" assembly="NHListTest" default-lazy="false">

  <class name="FirstClass" table="FirstClass">
    <id name="Id" unsaved-value="00000000-0000-0000-0000-000000000000" access="field">
      <generator class="guid.comb">
      </generator>
    </id>
    <list name="Numbers" generic="true"  table="Numbers" access="field">
      <key column="ParentId" />
      <index column="idx" />
      <element type="Decimal" column="Value"   />
    </list>
  </class>

  <class name="SecondClass" table="SecondClass">
    <id name="Id" unsaved-value="00000000-0000-0000-0000-000000000000" access="field">
      <generator class="guid.comb">
      </generator>
    </id>
    <list name="Numbers" generic="true"  table="Numbers" access="field">
      <key column="ParentId" />
      <index column="idx" />
      <element type="Decimal" column="Value"   />
    </list>
  </class>
 
</hibernate-mapping>


I'm hoping there's just something I've missed from the mapping file to allow it. Thanks in advance for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 08, 2007 10:59 am 
Newbie

Joined: Mon Oct 29, 2007 6:13 am
Posts: 2
Answering my own question...

Adding inverse="true" to the collection seems to get rid of the constraint.


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.