Hi!
I've a little problem! I'm trying to realize a web application in VB.net, and I'm mapping 2 classes, autors (Autore), and artworks (Opera): they are 'many to many' related objects; so in the db I've three tables: A_AUT, autors, DN_OG, artworks, DR_AUT, table with associations.
Any suggestions?
Thank you,
Matteo Figus
Hibernate version: NHibernate 1.2.0
Mapping documents:
Autore.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHWebSite" assembly="NHWebSite">
<class name="NHWebSite.NHWebSite.Autore, NHWebSite" table="A_AUT">
<id name="COD_AUT">
<column name="COD_AUT" sql-type="int(4)" not-null="true"/>
<generator class="increment" />
</id>
<property name="Name" column="Name" type="string" not-null="true" />
<set name="Opere" table="DR_AUT" lazy="true" inverse="true" cascade="all">
<key column="COD_AUT"/>
<many-to-many column="COD_OG" class="NHWebSite.NHWebSite.Opera, NHWebSite"/>
</set>
</class>
</hibernate-mapping>
Opera.hbm.xmlCode:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHWebSite" assembly="NHWebSite">
<class name="NHWebSite.NHWebSite.Opera, NHWebSite" table="DN_OG">
<id name="COD_OG">
<column name="COD_OG" sql-type="int(4)" not-null="true"/>
<generator class="increment" />
</id>
<property name="SGTT_OG" column="SGTT_OG" type="string" not-null="true" />
<set name="Autori" table="DR_AUT" lazy="true" inverse="true" cascade="all">
<key column="COD_OG"/>
<many-to-many column="COD_AUT" class="NHWebSite.NHWebSite.Autore, NHWebSite"/>
</set>
</class>
</hibernate-mapping>
Classes:Autore.vbCode:
Namespace NHWebSite
Public Class Autore
Private _COD_AUT As Integer
Private _Name As String
Private _Opere As IList(Of Opera)
Public Overridable Property COD_AUT()
Get
Return _COD_AUT
End Get
Set(ByVal value)
_COD_AUT = value
End Set
End Property
Public Overridable Property Name()
Get
Return _Name
End Get
Set(ByVal value)
_Name = value
End Set
End Property
Public Overridable Property Opere()
Get
Return _Opere
End Get
Set(ByVal value)
_Opere = value
End Set
End Property
End Class
End Namespace
Opera.vbCode:
Namespace NHWebSite
Public Class Opera
Private _COD_OG As Integer
Private _SGTT_OG As String
Private _Autori As IList(Of Autore)
Public Overridable Property COD_OG()
Get
Return _COD_OG
End Get
Set(ByVal value)
_COD_OG = value
End Set
End Property
Public Overridable Property SGTT_OG()
Get
Return _SGTT_OG
End Get
Set(ByVal value)
_SGTT_OG = value
End Set
End Property
Public Overridable Property Autori()
Get
Return _Autori
End Get
Set(ByVal value)
_Autori = value
End Set
End Property
End Class
End Namespace
NHibernateHelper.vbCode:
Imports System
Imports System.Web
Imports NHibernate
Imports NHibernate.Cfg
Namespace NHWebSite
Public NotInheritable Class NHibernateHelper
Public Const CurrentSessionKey As String = "nhibernate.current_session"
Public Shared ReadOnly sessionFactory As ISessionFactory
Shared Sub New()
Dim x As New NHibernate.Cfg.Configuration
sessionFactory = x.Configure.BuildSessionFactory()
End Sub
Public Shared Function GetCurrentSession() As ISession
Dim context As HttpContext = HttpContext.Current
Dim currentSession As ISession = context.Items(CurrentSessionKey)
If currentSession Is Nothing Then
currentSession = sessionFactory.OpenSession()
context.Items(CurrentSessionKey) = currentSession
End If
Return currentSession
End Function
Public Shared Sub CloseSession()
Dim context As HttpContext = HttpContext.Current
Dim currentSession As ISession = context.Items(CurrentSessionKey)
If currentSession Is Nothing Then Return
currentSession.Close()
context.Items.Remove(CurrentSessionKey)
End Sub
Public Shared Sub CloseSessionFactory()
If sessionFactory IsNot Nothing Then sessionFactory.Close()
End Sub
End Class
End Namespace
Code between sessionFactory.openSession() and session.close(): None for now. Just mapped and tryin to build without errors.
Full stack trace of any exception that occurs:"
identifier mapping has wrong number of columns: Autore type: Object "
Code:
[MappingException: identifier mapping has wrong number of columns: Autore type: Object]
NHibernate.Mapping.RootClass.Validate(IMapping mapping) +203
NHibernate.Cfg.Configuration.Validate() +224
NHibernate.Cfg.Configuration.BuildSessionFactory() +41
NHWebSite.NHWebSite.NHibernateHelper..cctor() in C:\Users\Matteo\Documents\Visual Studio 2005\Projects\NHWebSite\NHWebSite\Repository\NHibernateHelper.vb:14
[TypeInitializationException: L'inizializzatore di tipo di 'NHWebSite.NHWebSite.NHibernateHelper' ha generato un'eccezione.]
NHWebSite.NHWebSite.NHibernateHelper.GetCurrentSession() in C:\Users\Matteo\Documents\Visual Studio 2005\Projects\NHWebSite\NHWebSite\Repository\NHibernateHelper.vb:25
NHWebSite._Default.Page_Load(Object sender, EventArgs e) in C:\Users\Matteo\Documents\Visual Studio 2005\Projects\NHWebSite\NHWebSite\Default.aspx.vb:12
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
Name and version of the database you are using: Ms sql server 2005
Tables:
A_AUT
- COD_AUT - int(4) - (Primary Key)
- Name - nchar(50)
DN_OG
- COD_OG - int(4) - (Primary Key)
- SGTT_OG - nchar(50)
DR_AUT
- COD_AUT - int(4) - (Primary Key)
- COD_OG - int(4) - (Primary Key)