NHibernate Simple VB.Net Example Tutorial






2.10/5 (4 votes)
NHibernate using VB.Net
Introduction
This article will help you create a simple VB.Net project using NHibernate
Background Information
Files Required:
1.) Download NHibernate1.2.1.GA.msi and install it in your computer.
This installation will contain nhibernate.dll files for .net framework 1.1 and 2.0 .
2.) Create a new project (Web or console or windows application)
3.) Add Reference Nhibernate.dll and NHibernate.Mapping.Attributes
4.) Create a Project with the name EmployeeManagement (or) anything you wish
Using the code
Employee.VB :
// Create a Employee class with corresponding get and set methods Namespace Domain Public Class Employee Private _EmpID As Integer Private _firstname As String Private _lastname As String Private _age As String Private _address As String Public Property EmpID() As Integer Get Return _EmpID End Get Set(ByVal Value As Integer) _EmpID = Value End Set End Property Public Property firstname() As String Get Return _firstname End Get Set(ByVal Value As String) _firstname = Value End Set End Property Public Property lastname() As String Get Return _lastname End Get Set(ByVal Value As String) _lastname = Value End Set End Property Public Property age() As Integer Get Return _age End Get Set(ByVal Value As Integer) _age = Value End Set End Property Public Property address() As String Get Return _address End Get Set(ByVal Value As String) _address = Value End Set End Property End Class End Namespace
Employee.hbm.xml: (Make sure you set the compile option as embedded Resource)
<?xml version="1.0"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> <class name="EmployeeManagement.Domain.Employee,EmployeeManagement" lazy="false"> <id name="EmpID" type="integer" column="ID"> <generator class="native" /> </id> <property name="firstname" type="string" column="FirstName"/> <property name="lastname" type="string" column="LastName"/> <property name="age" type="integer" column="Age"/> <property name="address" type="string" column="Address"/> </class> </hibernate-mapping>
Web.Config (Or) App.Config:
<!-- Copy this code into your web.config or App.Config just below <configuration> tag --> <configSections> <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System,Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </configSections> <nhibernate> <add key="hibernate.show_sql" value="false" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.connection.connection_string" value="************<Your dbconnectionstring>********************" /> </nhibernate>
DbSession.vb: (This class creates the session and takes care of saving the data to the database)
Imports System Imports System.Reflection Imports NHibernate Imports NHibernate.Cfg Namespace NhibernateDB Public Class DbSession Public Sub SaveEmployeeToDatabase(ByVal emp As Domain.Employee) Dim session As ISession Dim transaction As ITransaction Try session = OpenSession() transaction = session.BeginTransaction() session.Save(emp) transaction.Commit() Catch ex As Exception Throw ex Finally session.Close() transaction = Nothing session = Nothing End Try End Sub Public Function OpenSession() As ISession Dim myConfig As NHibernate.Cfg.Configuration = New NHibernate.Cfg.Configuration Try myConfig.AddAssembly("EmployeeManagement") Dim SessionFactory As ISessionFactory = myConfig.BuildSessionFactory() Return SessionFactory.OpenSession() Catch ex As Exception Throw ex End Try End Function End Class End Namespace
Button click event to invoke the save function
Dim empObj As New Domain.Employee
Dim dbObj As New NhibernateDB.DbSession
Try
empObj.firstname = firstname.Text
empObj.lastname = lastname.Text
If Not age.Text.Trim = "" Then
empObj.age = Convert.ToInt32(age.Text)
Else
empObj.age = 0
End If
empObj.address = address.Text
dbObj.SaveEmployeeToDatabase(empObj)
Catch ex As Exception
Throw ex
Finally
empObj = Nothing
dbObj = Nothing
End Try