-->
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.  [ 6 posts ] 
Author Message
 Post subject: Get<IFoo> can't find Foo
PostPosted: Fri Mar 14, 2008 3:16 pm 
Newbie

Joined: Fri Mar 14, 2008 3:01 pm
Posts: 8
Location: Marysville, PA
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:1.2.1

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="NHibernate.Prototype.Domain"
                   assembly="NHibernate.Prototype"
                   default-access="field"
                   default-lazy="false"
                   default-cascade="none">
  <class name="Foo" table="foo">
    <id name="id">
      <generator class="identity" />
    </id>
  </class>
</hibernate-mapping>


simple console app
Code:
Configuration configuration = new Configuration().Configure();
            IFoo[] listOfFoos = new IFoo[] { new Foo(), new Foo(), new Foo() };

            IFoo found;
            using (ISessionFactory sessionFactory = configuration.BuildSessionFactory())
            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    foreach (IFoo foo in listOfFoos) session.Save(foo);
                    transaction.Commit();
                }
                found = session.Get<Foo>(2L);
            }

            foreach (IFoo foo in listOfFoos) Console.WriteLine("{2} {0} = 'found' {1}", foo.Id, foo == found, foo.GetType());
            Console.ReadKey();


Full stack trace of any exception that occurs:
"Unknown entity class: NHibernate.Prototype.Domain.IFoo"

" at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type theClass)\r\n at NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass)\r\n at NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation)\r\n at NHibernate.Impl.SessionImpl.Get(Type clazz, Object id)\r\n at NHibernate.Impl.SessionImpl.Get[T](Object id)\r\n at NHibernate.Prototype.Program.Main() in C:\\JMeckley\\My Documents\\Visual Studio 2005\\Projects\\NHibernate.Prototype\\NHibernate.Prototype\\Program.cs:line 23\r\n at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ThreadHelper.ThreadStart()"

Name and version of the database you are using: .\SqlExpress

Debug level Hibernate log excerpt:0

I'm brand new to NH. Just started this week. after reading the docs, and various online blogs I wrote the simple console app above to begin to experimenting with the framework.

I want to program to interfaces, my mapping is for a class of Foo not the interface IFoo (1 id column) I can save new instances of IFoo without a problem. however when I fetch an IFoo the get the exception above stating it doesn't know how to fetch IFoo. If I change this to Get<Foo> everything works as expected.

here are the Foo objects:
Code:
namespace NHibernate.Prototype.Domain
{
    public interface IFoo
    {
        long Id { get; }
    }

    public class Foo : IFoo
    {
        private long id;

        public Foo()
        {
        }

        public long Id
        {
            get { return id; }
        }

        public override string ToString()
        {
            return string.Format("Foo {0}", id);
        }
    }
}

is it possible to use Get<IFoo>(2L) without explicitly defining IFoo in an hbm? If not, can I do this without a descriminator column for the table? Am I way off base in what I'm attempting to do?

thank you
Jason


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 3:54 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Have you read this chapter in the documentation ?

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/inheritance.html

There're are ways where you can map IFoo without a descriminator, but than you need either a table per subclass or a table-per-concrete-class strategy.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 15, 2008 11:48 am 
Newbie

Joined: Fri Mar 14, 2008 3:01 pm
Posts: 8
Location: Marysville, PA
Yes, I read this and I thought I was doing it correctly.
Right now this is my goal

1. entities should only have public getters for the Id.
2. entities have an interface and concrete class.
3. I would like to map NHibernate to private/protected fields rather than public accessors.

So at this time I can save to the database via interfaces.
Code:
IFoo foo = new Foo();
session.Save(foo);


I just did some experimentation with a new concrete class Foo2 NHibernate could not map this object. I tried both implemenations
Code:
public class Foo2
{
   private IFoo foo;
   public Foo2(IFoo foo)
   {
      this.foo = foo;
   }
   public long Id { get { return foo.Id; } }
}

public class Foo2
{
   private long id;

   public long Id { get { return id; } }
}

There is something I'm missing in my understanding of how NHibernate works.

so now my understanding is that I have 2 options for interface mappings
1. have 1 table per concrete class so if I have IFoo, Foo and Foo2 then i will have 3 tables.
2. have 1 table with a discriminator value which means the sub0classed fields cannot be not null and i will have an extra column in the table to define types.

then somehow in code I would need to filter based on the discriminator value.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 3:05 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
That's the way I understand it. But normally you never use the discriminator in your application directly. In the application you can "filter" by class type.

You're new sample looks a bit strange for me. In the second one, you don't implement IFoo, but that may just be missing in the post and when you use IFoo as a member in your class, you have to map it different.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 8:43 am 
Newbie

Joined: Fri Mar 14, 2008 3:01 pm
Posts: 8
Location: Marysville, PA
forgot to inherit the interface
Code:
public class Foo2: IFoo ...

I thought of another way to handle, although it's not my prefered method.
I could use abstract classes with private fields instead of interfaces.

Personally i like how interfaces are "cleaner" than abstract classes. and i like that you cannot apply implmenting code to interfaces.

maybe another, more complex route, would be to have DAO objects which are mapped to NHibernate and domain objects, which can be whatever I need them to be. this may be the better way to go, because then i can extend/decorate implementations of IFoo and the DAO interfaces won't change.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 4:01 pm 
Newbie

Joined: Fri Mar 14, 2008 3:01 pm
Posts: 8
Location: Marysville, PA
I finally got this working. with some utitlities from Rhino-Tools. I don't think what I wanted was directly accessible from NHibernate. However Rhino-Tools has a Repository<T> object which uses detached criteria to FindOne().

There was alot of forking, and compiling to get this all to work as Rhino-Tools doesn't work directly against NH 1.2.1 or NH 2.0 alpha


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.