-->
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.  [ 11 posts ] 
Author Message
 Post subject: joined-subclass problem
PostPosted: Wed Feb 21, 2007 12:55 pm 
Newbie

Joined: Tue Feb 06, 2007 5:59 pm
Posts: 14
<class name="hTest.User" table="User">
<id name="CategoryId" column="CategoryId" unsaved-value ="0">
<generator class="native" />
</id>
<property name="Name" column="Name" />
<joined-subclass name="hTest.AdvUser" table="AdvUser" lazy="true">
<key column="Address_Id"/>
<property name="info" column="info"/>
</joined-subclass>
</class>


In the code,
User c = new User();
c.Name = "xxx";
...
session.Save(c);
...

AdvUser c = new AdvUser();
c.Name = "xxx22";
c.info = "xxx22";
...
session.Save(c);

It works fine, I can save whatever I want ,
and it works on AdvUser


However IF the table is empty
then I just create AdvUser,
It will keep overwrite the Id =1 record,
it wont save a new one

but after I create User, then save back , the database works fine again, it seems strange to me, any idea

Does anyone have this problem ?

_________________
www.pakmailmarkham.ca
www.digitmind.com
www.fineartcollectors.com


Top
 Profile  
 
 Post subject: Re: joined-subclass problem
PostPosted: Thu Feb 22, 2007 4:07 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
cc96ai wrote:
<class name="hTest.User" table="User">
<id name="CategoryId" column="CategoryId" unsaved-value ="0">
<generator class="native" />
</id>
<property name="Name" column="Name" />
<joined-subclass name="hTest.AdvUser" table="AdvUser" lazy="true">
<key column="Address_Id"/>
<property name="info" column="info"/>
</joined-subclass>
</class>

However IF the table is empty
then I just create AdvUser,
It will keep overwrite the Id =1 record,
it wont save a new one

but after I create User, then save back , the database works fine again, it seems strange to me, any idea

Does anyone have this problem ?

The behaviour of "native" generator depends on database in use, so explanation would depend on that.

But assuming MS SQL, the Id of saved object is whatever the CategoryId identity column of table User returns after insert.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:35 am 
Newbie

Joined: Tue Feb 06, 2007 5:59 pm
Posts: 14
I am usiny MySQL ,

NHibernate: INSERT INTO User(Name) VALUES (?p0)
?p0 = 'Test Name'
NHibernate: SELECT LAST_INSERT_ID()
NHibernate: INSERT INTO AdvUser(info, Address_Id) VALUES (?p0, ?p1)
?p0 = 'TESTInfo'
?p1 = '1'

I always get Id = 1 , on get next id

However , If i only insert User , (not AdvUser)
I will get ?p1='2'

_________________
www.pakmailmarkham.ca
www.digitmind.com
www.fineartcollectors.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 3:32 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
cc96ai wrote:
NHibernate: SELECT LAST_INSERT_ID()


AFAIK NHibernate uses the value returned by this as an ID. So, it is up to Your database server do return correct value.

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 4:06 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
If you are using 1.2.0.Beta3, try upgrading to CR1. There was a bug in Beta3 that caused "SELECT LAST_INSERT_ID()" to execute in a different connection/transaction than the actual insert statement so last_insert_id() could return a wrong value.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 10:42 am 
Newbie

Joined: Tue Feb 06, 2007 5:59 pm
Posts: 14
I updated into CR1

the problem come worse ,

even I created the base object
User c = new User();

it will keep overwrite the exisitng one

_________________
www.pakmailmarkham.ca
www.digitmind.com
www.fineartcollectors.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 10:45 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Post your class definitions please, both User and AdvUser.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 10:46 am 
Newbie

Joined: Tue Feb 06, 2007 5:59 pm
Posts: 14
I set hbm2ddl.auto into hibernatae.cfg.xml

<property name="hbm2ddl.auto">create</property>

it seems to create the table everytime ?

because I disable this property, it works ,

any idea how to use hbm2ddl.auto ?

I want to create the table in the 1st time insert

_________________
www.pakmailmarkham.ca
www.digitmind.com
www.fineartcollectors.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 11:16 am 
Newbie

Joined: Tue Feb 06, 2007 5:59 pm
Posts: 14
User.hbm.xml
----------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" assembly="digi">
<class name="hTest.User" table="User">
<id name="UserId" column="UserId" unsaved-value ="0">
<generator class="native" />
</id>
<property name="Name" column="Name" />

<joined-subclass name="hTest.AdvUser" table="AdvUser" lazy="true">
<key column="AdvUser_Id" foreign-key="UserId"/>
<property name="Info" column="Info"/>
</joined-subclass>
</class>
</hibernate-mapping>

--------------------------------------------
User.cs

public class User
{
private int _userId;
private string _name;

public virtual string UserId
{
get { return _userId; }
set { _userId = value; }
}

public virtual string Name
{
get { return _name; }
set { _name = value; }
}

}
--------------------------------------------


AdvUser.cs
------------
public class AdvUser: User
{
private string _info;

public AdvUser()
{
}

public virtual string Info
{
get { return _info; }
set { _info = value; }
}
}

_________________
www.pakmailmarkham.ca
www.digitmind.com
www.fineartcollectors.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 11:41 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I assume that "public virtual string UserId" is a typo.

hbm2ddl.auto creates tables when the session factory is first created. It seems to me that you are creating more than one session factory in your code, that is unnecessary.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 23, 2007 12:35 pm 
Newbie

Joined: Tue Feb 06, 2007 5:59 pm
Posts: 14
here is the main program ,
I run the program in command line to test


Helper.cs
---------
public class Helper
{
public static string strLoggerCfg = "digi";
public static string strHibernateCfg = "digi";
private static ISessionFactory sessionFactory;
private static Configuration configuration;
private static readonly ILog logger = LogManager.GetLogger(strLoggerCfg);

/*
* Get Hibernate Factory
* getSessionFactory()
* - return ISessionFactory
*/
public static ISessionFactory getSessionFactory(){
try {
if ( sessionFactory == null ){
sessionFactory = getConfiguration().BuildSessionFactory();
}
return sessionFactory;
}
catch (HibernateException e){
getLogger().Fatal("--getSessionFactory()--");
getLogger().Fatal(e.ToString());
return null;
}
}

/*
* Get Hibernate Configuration
* getConfiguration()
* - return Configuration
*/
public static Configuration getConfiguration(){
try{
if (configuration == null){
configuration = new Configuration()
.Configure()
.AddAssembly(strHibernateCfg);
}
return configuration;
}
catch( HibernateException e){
getLogger().Fatal("--getConfiguration()--");
getLogger().Fatal(e.ToString());
return null;
}
}
public static ILog getLogger()
{
XmlConfigurator.Configure();
return logger;
}
}


Program.cs
----------
static void Main(string[] args)
{
ILog logger = Helper.getLogger();

User c = new User();
c.Name = "xxx";

try
{
ISession session = Helper.getSessionFactory().OpenSession();

ITransaction transaction = session.BeginTransaction();

session.Save(c);
transaction.Commit();
session.Close();
}
catch (HibernateException e)
{
logger.Fatal(e.ToString());
}
}

_________________
www.pakmailmarkham.ca
www.digitmind.com
www.fineartcollectors.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.