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! ;)