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.  [ 8 posts ] 
Author Message
 Post subject: NHibernate 2 Annotations: only 1st GeneratorAttribute used
PostPosted: Wed Oct 25, 2006 8:51 am 
Newbie

Joined: Wed Oct 25, 2006 8:45 am
Posts: 9
I tried posting a bug report on jira but jira currently has its own bugs :)

I have a simple program with 2 classes which use annotations. Both have an [Id] property with [Generator] annotation, however, when I called Serialize(Assembly) only the first one gets the <generator> element in the generated xml.

I tried investigating further, and if I call Serialize(Type) for each class in turn, the same happens - whichever class gets serialized serialized first gets the <generator> element, and the second does not.

I tried this with both the beta1 and svn versions. The classes have a simple structure as follows:

Code:
[Class]
public class Department : BaseRow
{
    private int _ID;
    [Id(Column = "dept_pk", Name="ID", UnsavedValue = "0")]
    [Generator(Class = "native")]
    public override int ID
    {
        get { return this._ID; }
        set { this._ID = value; }
    }

    private string _Name;
    [Property(Column="dept_name")]
    public virtual string Name
    {
        get { return this._Name; }
        set { this._Name = value; }
    }

    public override string Label
    {
        get { return Name; }
    }
}


Top
 Profile  
 
 Post subject: More information...
PostPosted: Wed Oct 25, 2006 9:51 am 
Newbie

Joined: Wed Oct 25, 2006 8:45 am
Posts: 9
It seems that the cause of this error is related to the default constructors of IdAttribute and GeneratorAttribute (and probably others). These both set the _position variable in BaseAttribute to zero. When the HbmWriter class calls GetSortedAttributes(member), the positions are identical so the order could be anything, which means the IdAttribute could come AFTER the GeneratorAttribute, causing it not to be written.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 8:59 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Solution:
Code:
[Id(...)]
[Generator(1, ...)]


Please, read the documentation.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 9:35 am 
Newbie

Joined: Wed Oct 25, 2006 8:45 am
Posts: 9
KPixel wrote:
Solution:
Code:
[Id(...)]
[Generator(1, ...)]


Please, read the documentation.


Thanks, it worked. But... what documentation? The NHibernate reference doesn't say much about annotations. Is there some other document somewhere which I missed?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 28, 2006 5:17 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
The documentation coming with NHibernate has a section about NHibernate.Mapping.Attributes.

Here is the online version, note that it is for the version 1.0.2. For the v1.2.0.Beta1, read the one coming with NHibernate.

Here is the link to all NHibernate-related documentation (Don't miss the NHibernate Resources). :)

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


Top
 Profile  
 
 Post subject: Please document the nhibernate annotations
PostPosted: Thu Jan 04, 2007 10:31 am 
Beginner
Beginner

Joined: Tue Jan 02, 2007 5:53 pm
Posts: 42
Location: Bergen, Norway
First of all I would like to say that I really like the NHibernate project and I enjoy developing with nHibernate (and spring.net). Being able to automate, build and update the database based on my annotations of my domain classes is very cool, and a major time saviour. Although I find the nHibernate documentation to be fairly good, I must say that the documentation of annotations is very poor, if not almost lacking. The examples I have found (and I have searched the last 6th months or so, and also looked at the content provided by the links above) only covers the very basics, not the more difficult many-to-many and one-to-many mappings for a variety of collections.

To be honest, I find this documentation to be extremly incomplete. The nHibernate resouces does not help much either as almost all examples everywhere use hbm.xml files or if they use annotations, the examples are almost always very basic. The hbm.xml syntax is quite different from the annotation's syntax. Currently I am trying to decipher the nhibernate.annotations.test whenever I wonder how to map something. Have a look at the very intuitive classes!

Creating tests and examples this way is not very fruitful. I cannot understand why anyone would cook up something like this anyway. It is extremly difficult to get anything out of the tests. Why not explain the usages of annotations similar to the java documentation found here?

I would gladly write a chapter on how to use annotations if I only knew more about how to use them. Currently I get by using:
    class
    subclass
    id and generator
    property
    many-to-one
    bag with one-to-many
    bag with many-to-many


This is of course not enough, I would like to use many more, but I haven't been successful in applying lists, maps or collections in my one-to-many or many-to-many mappings so far (and due to time constraints I haven't had time to investigate what I've specified wrong either, using bag solved the problem there and then...). I hope to see the annotations better documented in the future!

_________________
Cheers,
Steinar.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 11:28 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
I agree that its documentation could be much better. However, I don't have time to work on it now...

I would be grateful if you could write something to improve the current one. And you can use Hibernate Annotations documentation as a source of inspiration :)

Let me know if I can help you in any way.

Note that using NHibernate.Mapping.Attributes is just like using XML mapping. If you can write a mapping using XML, you can easily convert it in attributes (just make sure that you correctly set the positions).

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 05, 2007 5:03 am 
Beginner
Beginner

Joined: Tue Jan 02, 2007 5:53 pm
Posts: 42
Location: Bergen, Norway
Hi!

Sure, I will try to write a few simple pono classes, use as many different kinds of annotations as possible and start writing a corresponding documentation with code excerpt. If you like I can bundle a very simple test-app (that you could put in your EG project), that can be downloaded and played with as well.

Where do you want this documentation to reside? Maybe Here, as you would probably like to modify, supply, extend and update it?

I think it might be smart to use the Hibernate Annotation documentation for inspiration as it might ease and speed up the work of producing the documentation. What do you think, email me at: steinar.dragsnes (at) gmail.com

_________________
Cheers,
Steinar.


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