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.  [ 15 posts ] 
Author Message
 Post subject: Mapping.Attributes and Inheritance
PostPosted: Tue Aug 01, 2006 9:32 am 
Beginner
Beginner

Joined: Wed Jul 19, 2006 8:24 am
Posts: 35
Is inheritance possible with the NHibernate.Mapping.Attirbutes contrib?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 10:24 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, it should work.


Top
 Profile  
 
 Post subject: Where to the attributes go?
PostPosted: Wed Aug 02, 2006 10:11 am 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
sergey wrote:
Yes, it should work.


If I have a DomainBase class that defines ID and a User class that inherits DomainBase, where do I put the attribute for ID?

In the database, the column name for ID is UserID, RoleID, XxxxID, etc.


Top
 Profile  
 
 Post subject: Re: Where to the attributes go?
PostPosted: Wed Aug 02, 2006 2:54 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
anortham wrote:
If I have a DomainBase class that defines ID and a User class that inherits DomainBase, where do I put the attribute for ID?

In the database, the column name for ID is UserID, RoleID, XxxxID, etc.


NHibernate.Mapping.Attributes mimics XML mapping; so if you can figure out how to write your mapping using XML, you can easily write it using attributes.

Note that the project NHibernate.Mapping.Attributes.Test contains classes mapped using most attributes; you can take a look to see how it is done...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: Re: Where to the attributes go?
PostPosted: Wed Aug 02, 2006 6:30 pm 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
KPixel wrote:
anortham wrote:
If I have a DomainBase class that defines ID and a User class that inherits DomainBase, where do I put the attribute for ID?

In the database, the column name for ID is UserID, RoleID, XxxxID, etc.


NHibernate.Mapping.Attributes mimics XML mapping; so if you can figure out how to write your mapping using XML, you can easily write it using attributes.

Note that the project NHibernate.Mapping.Attributes.Test contains classes mapped using most attributes; you can take a look to see how it is done...


I can get it to work in XML mapping no problem. The problem begins when I have a base class like this:

public abstract class PersistentObjectBase
{
private int id;

[Id( 0, Name = "ID", TypeType = typeof ( int ),
UnsavedValue = "0" )]
[Generator( 1, Class = "identity" )]
public virtual int ID
{
get { return id; }
set { id = value; }
}
}

Now add this class :

[Class( 0, NameType = typeof ( AppUser ) )]
public class AppUser : PersistentObjectBase
{
[Cache( 1, Usage = CacheUsage.ReadWrite )]

public AppUser() : base() {}

}

Where am I supposed to define the column name now? It's different per class so I can't define it in the base class.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 8:38 am 
Beginner
Beginner

Joined: Wed Jul 19, 2006 8:24 am
Posts: 35
bump!


Top
 Profile  
 
 Post subject: Any ideas?
PostPosted: Fri Aug 04, 2006 10:08 am 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
Does anyone have any input on this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 10:23 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
ATM, the only solution is to move the mapping attributes to "AppUser" and other classes (but the properties can stay in the base class).

This problem gave me an idea of how NHMA can be improved to nicely solve this problem.
Cf. http://jira.nhibernate.org/browse/NH-684
Hopefully, it should be in NH1.2.0.Alpha2.

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: The resulting xml isn't correct.
PostPosted: Fri Aug 04, 2006 10:36 am 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
KPixel wrote:
ATM, the only solution is to move the mapping attributes to "AppUser" and other classes (but the properties can stay in the base class).

This problem gave me an idea of how NHMA can be improved to nicely solve this problem.
Cf. http://jira.nhibernate.org/browse/NH-684
Hopefully, it should be in NH1.2.0.Alpha2.


Here's the changes:

public abstract class PersistentObjectBase
{
private int id;

public virtual int ID
{
get { return id; }
set { id = value; }
}
}


[Class( 0, NameType = typeof ( AppUser ) )]
public class AppUser : PersistentObjectBase
{
[Cache( 1, Usage = CacheUsage.ReadWrite )]

public AppUser() : base() {}

[Id( 0, Name = "ID", TypeType = typeof( int ), Column = "AppUserID", UnsavedValue = "0" )]
[Generator( 1, Class = "identity" )]

}


When I try that, the resulting xml looks like this:

<class name="Model.AppUser, Model" table="AppUser">
<cache usage="read-write" />
<id name="ID" column="AppUserID" type="Int32" unsaved-value="0" />

It doesn't add the [Generator( 1, Class = "identity" )] to the stream which throws an exception "Could not configure datastore from input stream."


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 04, 2006 4:48 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
There is something wrong with your mapping...
Can you post the whole class AppUser?

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: mapping
PostPosted: Tue Aug 08, 2006 10:08 am 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
KPixel wrote:
There is something wrong with your mapping...
Can you post the whole class AppUser?


The class is pretty big, but the thing is the mappings all work fine. When it looks like this:

Code:
[Id( 0, Name = "ID", TypeType = typeof ( int ), Column = "AppUserID", UnsavedValue = "0" )]
[Generator( 1, Class = "identity" )]
public virtual int ID
{
     get { return id; }
     set {id = value; }
}

[Bag( 0, Name = "AppRoles", Table = "AppUserRole", Inverse = false, Lazy = true, Cascade = CascadeStyle.None )]
[Cache( 1, Usage = CacheUsage.ReadWrite )]
[Key( 2, Column = "AppUserID" )]
[ManyToMany( 3, ClassType = typeof ( AppRole ), Column = "AppRoleID" )]
public virtual IList<AppRole> AppRoles
{
    get { return appRoles; }
    set { appRoles = value; }
}


it works fine. But for some reason when you remove the ID property, the second part of the attribute never makes it into the mapping:

Code:
[Id( 0, Name = "ID", TypeType = typeof ( int ), Column = "AppUserID", UnsavedValue = "0" )]
[Generator( 1, Class = "identity" )]

[Bag( 0, Name = "AppRoles", Table = "AppUserRole", Inverse = false, Lazy = true, Cascade = CascadeStyle.None )]
[Cache( 1, Usage = CacheUsage.ReadWrite )]
[Key( 2, Column = "AppUserID" )]
[ManyToMany( 3, ClassType = typeof ( AppRole ), Column = "AppRoleID" )]
public virtual IList<AppRole> AppRoles
{
     get { return appRoles; }
     set { appRoles = value; }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 7:56 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
You can either put the Id (and Generator) on another property or renumber following attributes: [Bag(2)]...

By the way, I just added a new feature to fix your problem:
Cf. http://jira.nhibernate.org/browse/NH-684
So you can grap the SVN version and use it :)

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 8:56 am 
Beginner
Beginner

Joined: Wed Jul 19, 2006 8:24 am
Posts: 35
Thanks for fixing the problem KPixel. That was the same issue I was having.


Top
 Profile  
 
 Post subject: Thanks.
PostPosted: Wed Aug 09, 2006 9:42 am 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
KPixel wrote:
You can either put the Id (and Generator) on another property or renumber following attributes: [Bag(2)]...

By the way, I just added a new feature to fix your problem:
Cf. http://jira.nhibernate.org/browse/NH-684
So you can grap the SVN version and use it :)


Great! Thanks.


Top
 Profile  
 
 Post subject: Got it.
PostPosted: Wed Aug 09, 2006 2:11 pm 
Beginner
Beginner

Joined: Fri Jul 14, 2006 1:51 pm
Posts: 27
I got it plugged in and it's working great, thanks again.


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