getting NHibernate and Oracle to work

35 views
Skip to first unread message

sebi

unread,
Apr 23, 2008, 3:28:56 AM4/23/08
to nhusers
Hi folks,

Sorry to unnerve everyone with this newbie stuff, but I just can't get
my Oracle persistence hibernated...
I really wanted to get something like a quickstart example working in
order to see the benefits of NH, but i fail at a very early stage.

I worked through many NH-tutorials, but the problems I have here seem
oracle-related.
I will try to put everything here in order to shed some light on my
problem.

/**********nhibernate.config**********/
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property
name="connection.provider">NHibernate.Connection.DriverConnectionProvider</
property>
<property name="dialect">NHibernate.Dialect.Oracle9Dialect</
property>
<property
name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</
property>
<property name="connection.connection_string">Data
source=host.name;User Id=dbuser;Password=dbpass;</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="query.substitutions">True=1;False=0</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>

/**********user class**********/
namespace NHibernate.Examples.QuickStart
{
[NHibernate.Mapping.Attributes.Class(0,
Table = "users",
Schema="sc01")]
public class User
{
[NHibernate.Mapping.Attributes.Id(0,
Column = "logonid",
Name = "logonId",
TypeType =
typeof(string),
UnsavedValue = "")]
public string logonId;

[NHibernate.Mapping.Attributes.Property(0,
Column = "name",
Name = "Name",
TypeType = typeof(string),
NotNull = true,
Length = 100)]
public string name;

}
}

/**********main**********/
namespace NHibernate.Examples.QuickStart
{
class Program
{
static void Main(string[] args)
{
try
{
NHibernate.Cfg.Configuration cfg = new
NHibernate.Cfg.Configuration();
cfg.Configure("C:\\path\\to\
\nhibernate.config");
ISessionFactory sf = cfg.BuildSessionFactory();
ISession ses = sf.OpenSession();
ITransaction ta = ses.BeginTransaction();

IList userList =
ses.CreateCriteria(typeof(User)).List();
foreach (User usr in userList)
{
Console.Out.WriteLine("Username " + usr.name);
}
ses.Close();
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message);
}
}
}
}

My Issues :

First of all, the driver resource (Oracle.DataAccess) could not be
ID'd. I have looked this up and tried to fully qualify the assembly by
adding to the app.config

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="Oracle.DataAccess"
fullName="Oracle.DataAccess, Version=2.0.50727.42,
Culture=neutral, PublicKeyToken=89b483f429c47342" />
</assemblyBinding>
</runtime>
</assemblyBinding>
</runtime>

No use. So I copied the oracle assembly into my bin/debug directory,
which did the trick. This is very very ugly...Is there anything wrong
with what I added to app.conf? Both Version and PTK have been
delivered by the sn.exe.

Next thing, after just copying the dll :

IList userList = ses.CreateCriteria(typeof(User)).List();

gives a compiler error, since IList is generic. All tutorials I have
seen do it like that, how can this be done without getting a compiler
error? I went for a one-liner to make it easier to find the bug :

User usr = ses.Load(typeof(User), 1);

This again leads to another exception : ses.Load returns an Object
that cannot be cast implicitly, so i have to go like

User usr = (User)ses.Load(typeof(User), 1);

That, again throws "unknown entity class
NHibernate.Examples.QuickStart.User", which I fail to grasp. Any Idea?

It would be really great if someone had the time to write a line or
two.

thanks in advance,

Sebi

sebi

unread,
Apr 23, 2008, 3:40:26 AM4/23/08
to nhusers
By the way, the "unknown entity class
NHibernate.Examples.QuickStart.User" exception only shows up when I
switch
the database driver to NHibernate.Driver.OracleClientDriver, using the
MS assembly.
With NHibernate.Driver.OracleDataClientDriver I will still get
"cannot open connection"

since this is all in assemblies and not in my code, i can't dig in
really deep.The connection string
is correct, i've used it before.

sebi

unread,
Apr 23, 2008, 4:03:24 AM4/23/08
to nhusers
Well, my own fault about the IList, I used System.Collections.Generic
instead of System.Collections.
Now, at least, I get an empty result set (while there's one dataset in
the oracle table).

On 23 Apr., 09:28, sebi <edelme...@gmail.com> wrote:

Gabriel Schenker

unread,
Apr 23, 2008, 4:27:40 AM4/23/08
to nhu...@googlegroups.com
you have to use
User user = ses.Get<User>(userId);
to get a single user (user will be null if not found). If you use User
user = Load<User>(userId) on the other hand an exception will be
thrown if a user with the given id cannot be found.
If you want to retrieve a list then use
IList<User> users = ses.CreateCriteria(typeof(User)).List<User>()

Is your oracle assemblies registered in the GAC? If not, then it is
"normal" to copy them local into the bin.

sebi

unread,
Apr 23, 2008, 5:40:13 AM4/23/08
to nhusers
Yes, oracle.dataaccess is in the GAC.
I don't mind living with a copy, if only it worked...I tried quite
some stuff, and I still get a "could not open connection"-Exception.
As for now, I use the OracleClientDriver, which i find unsatisfying.



On 23 Apr., 10:27, "Gabriel Schenker" <gnschen...@gmail.com> wrote:
> you have to use
> User user = ses.Get<User>(userId);
> to get a single user (user will be null if not found). If you use User
> user = Load<User>(userId) on the other hand an exception will be
> thrown if a user with the given id cannot be found.
> If you want to retrieve a list then use
> IList<User> users = ses.CreateCriteria(typeof(User)).List<User>()
>
> Is your oracle assemblies registered in the GAC? If not, then it is
> "normal" to copy them local into the bin.
>

sebi

unread,
Apr 23, 2008, 6:39:47 AM4/23/08
to nhusers
For once, I ignore the error proced by the Oracle assembly and try to
get the mapping going.
For that, I need to define the user class as an entity:

cfg.AddAssembly("NHibernate.Examples");
cfg.AddClass(typeof(User));

Since i don't want any hbm mappings, I also add


NHibernate.Mapping.Attributes.HbmSerializer.Default.Validate = true;

cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(

System.Reflection.Assembly.GetExecutingAssembly()
)
);

prior to building the session factory.I still get an error for a
missing User.hbm.xml. Why does that occur?

Gabriel Schenker

unread,
Apr 23, 2008, 9:43:43 AM4/23/08
to nhu...@googlegroups.com
To be shure that the HbmSerializer really generates the hbm's for you,
you better use this syntax
cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(typeof(User).Assembly));

eventually I would also let the HbmSerializer output the hbm's to a
file to check what is happening/generating

Nazareno

unread,
Apr 24, 2008, 8:34:55 AM4/24/08
to nhusers
Hello,

I have made the connection working it this way:

1) Change:

<property name="connection.driver_class">
NHibernate.Driver.OracleClientDriver
</property>

to

<property name="connection.driver_class">
NHibernate.Driver.OracleDataClientDriver
</property>

2) Changed the connection string (reference: www.connectionstrings.com)

3) Added to the App.Config:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="Oracle.DataAccess"
fullName="Oracle.DataAccess, Version=10.2.0.100,
Culture=neutral, PublicKeyToken=89b483f429c47342" />
</assemblyBinding>
</runtime>

The version and the PublicKeyToken can be checked from the GAC
directory.

Then it works (and really more faster than the Microsoft one).

Bye
Nazareno

Fabio Maulo

unread,
Apr 24, 2008, 7:08:21 PM4/24/08
to nhu...@googlegroups.com
I forgot to advise Karl to add it to NH wiki.
Thanks Nazareno.

--
Fabio Maulo

sebi

unread,
Apr 30, 2008, 7:53:38 AM4/30/08
to nhusers
thanks people. i got the wrong version of the driver...man, that's
dumb
Reply all
Reply to author
Forward
0 new messages