-->
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: NHibernate.HibernateException : Creating a proxy instance
PostPosted: Sat Dec 26, 2009 1:35 pm 
Newbie

Joined: Sat Dec 26, 2009 1:14 pm
Posts: 1
Hey,

I have come across some wierd behaviour, which i think might be because of the way i have implemented my code. I am trying to load an object using NHibertnate, but it gives me the following error:

failed: NHibernate.HibernateException : Creating a proxy instance failed
----> System.TypeLoadException : Access is denied: 'TeamMatchScoreController.DataObjects.Team'.

However there is a workaround to get rid of this error, but i dont think it should be necessary. First i will show you how i have implemented my code, and later explain how i can turn the error on and off. And where my problem lies.

app.config
Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
      <property name="default_schema">djc</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <property name="connection.connection_string">Database=djc;Data Source=localhost;User Id=djc;Password=djc</property>
      <property name="show_sql">true</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.isolation">ReadCommitted</property>
      <property name="use_proxy_validator">true</property>
      <property name="hbm2ddl.keywords">none</property>
      <mapping assembly="TeamMatchScoreController"/>
    </session-factory>
  </hibernate-configuration>
</configuration>


Team.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TeamMatchScoreController.DataObjects.Team, TeamMatchScoreController" table="teams">
    <id name="TeamName" column="team_name" type="String" length="75">
      <generator class="assigned" />
    </id>
    <bag name="PlayersList" cascade="all">
      <key column="team_name"/>
      <one-to-many
         class="TeamMatchScoreController.DataObjects.Player, TeamMatchScoreController"/>
    </bag>
  </class>
</hibernate-mapping>


Team.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace TeamMatchScoreController.DataObjects
{
    class Team
    {
        public virtual string TeamName { get; set; }
        public virtual IList<Player> PlayersList { get; set; }

        public Team()
        {
        }

        public virtual void addPlayer(Player player)
        {
            if (PlayersList == null)
            {
                PlayersList = new List<Player>();
            }

            PlayersList.Add(player);
        }
    }
}


Player.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TeamMatchScoreController.DataObjects.Player, TeamMatchScoreController" table="players">
    <id name="PlayerID" column="player_id">
      <generator class="assigned" />
    </id>
    <property name="PlayerName" column="player_name" type="String" length="75" />
    <many-to-one name="Team" class="TeamMatchScoreController.DataObjects.Team, TeamMatchScoreController" column="team_name" />
  </class>
</hibernate-mapping>


Player.cd
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace TeamMatchScoreController.DataObjects
{
    class Player
    {
        public virtual int PlayerID { get; set; }
        public virtual string PlayerName { get; set; }
        public virtual Team Team { get; set; }

        public Player()
        {
        }
    }
}


And finally here is the little piece of test code, i am using to test this with:

The DataAccess functions
Code:
private static ISessionFactory _sessionFactory;

        public DataAccess()
        {
        }

        private static ISessionFactory HibernateSessionFactory()
        {
            if (_sessionFactory == null)
            {
                var configuration = new Configuration();
                configuration.Configure();
                _sessionFactory = configuration.BuildSessionFactory();
            }
            return _sessionFactory;
        }

        public static ISession OpenHibernateSession()
        {
            if (_sessionFactory == null)
            {
                HibernateSessionFactory();
            }
            return _sessionFactory.OpenSession();
        }

        public static void CloseAndCommitHibernateSession(ISession session, ITransaction transaction)
        {
            session.Flush();
            transaction.Commit();
            session.Close();
        }


The load function
Code:
        public void TestLoadPlayer()
        {
            ISession session = DataAccess.OpenHibernateSession();
            ITransaction transaction = session.BeginTransaction();
            //The line below, is what triggers the error. If it is outcommented, then it fails. If i comment it in, i will work.
            //Team team = session.Get<Team>("Copenhagen");
            Player player = session.Get<Player>(1);
            DataAccess.CloseAndCommitHibernateSession(session, transaction);
            Assert.AreEqual("Lars Olafsson", player.PlayerName);
        }


As you can see above, in the comments in the code, the line "Team team = session.Get<Team>("Copenhagen");" is whats giving me the problems. If i remove this from the function it throws the error. But if i leave it in the code, it actually does exactly what i expect it to. But when i use this later on, i am not always certain which Team a Player is playing for, and is interested in being able to load a player, without having to load the Team first.

Is there a setting somewhere i have missed, or what can i do to get this functionality, if it is even possible?

Hope to receive an answer soon, knowing its Christmas an all! ;)


Top
 Profile  
 
 Post subject: Re: NHibernate.HibernateException : Creating a proxy instance
PostPosted: Wed Feb 08, 2012 7:39 pm 
Newbie

Joined: Wed Feb 08, 2012 7:35 pm
Posts: 1
The cause of this exception is that the classes "Team" and "Player" are by default "internal" since there is no access specifier on them. Redeclare them to be "public class Team" and "public class Player" and the exception should be resolved.


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.