Coder Social home page Coder Social logo

nhibernate / nhibernate.mapping.attributes Goto Github PK

View Code? Open in Web Editor NEW
21.0 17.0 9.0 5.59 MB

With NHibernate.Mapping.Attributes, you can use .NET attributes to decorate your entities and these attributes will be used to generate the mapping information. So you will no longer have to bother with these nasty XML files ;).

License: GNU Lesser General Public License v2.1

C# 99.98% Batchfile 0.02%

nhibernate.mapping.attributes's Introduction

What is NHibernate.Mapping.Attributes?

NHibernate requires mapping information to bind your domain model to your database. Usually, it is written (and maintained) in separated hbm.xml files.

With NHibernate.Mapping.Attributes, you can use .NET attributes to decorate your entities and these attributes will be used to generate the mapping information. So you will no longer have to bother with these nasty XML files ;).

Licensed under the terms of the GNU Lesser General Public License.

Build for NHibernate 4.0

Important breaking change: When using the [*Class] attributes, the property Name is automatically deduced only if Name and EntityName are both null. This is due to the fact that this property is optional since you can use EntityName instead. You can turn off this behavior by doing: ((HbmWriterEx) HbmSerializer.Default.HbmWriter).DoNotAutoDetectClassName = true;

Online information

Website

Documentation

Community Group (to ask questions)

Up-to-date source code available at GitHub

nhibernate.mapping.attributes's People

Contributors

diegojancic avatar fredericdelaporte avatar hazzik avatar jpenniman avatar rjperes avatar toshik avatar tunatoksoz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nhibernate.mapping.attributes's Issues

IdAttribute does not reflact it's Name attribute!

For example, this is my City class with attribute mapping:

[Class(Schema = "public", Table = "cities")]
public class City {

    [Id(Column = "id", Type = "long", Generator = "trigger-identity")]
    public virtual long Id { get; set; }

    [Property(Column = "name", Type = "string", Length = 64, NotNull = true, Unique = true)]
    public virtual string Name { get; set; }

    [Property(Column = "population", Type = "int", NotNull = false)]
    public virtual int Population { get; set; }

}

the xml mapping generated is:

<class table="cities" schema="public" name="WebTest.Entities.City, WebTest">
  <id column="id" type="long" generator="trigger-identity" />
  <property name="Name" type="string" column="name" length="64" not-null="true" unique="true" />
  <property name="Population" type="int" column="population" not-null="false" />
</class>

the mapping for id is not corrent, will cause the Id property in result will be 0, because it is not mapped.

the work around is specify Name for IdAttribute :

[Id(Name = "Id", Column = "id", Type = "long", Generator = "trigger-identity")]
public virtual long Id { get; set; }

then the xml generated is correct:

<class table="cities" schema="public" name="WebTest.Entities.City, WebTest">
  <id name="Id" column="id" type="long" generator="trigger-identity" />
  <property name="Name" type="string" column="name" length="64" not-null="true" unique="true" />
  <property name="Population" type="int" column="population" not-null="false" />
</class>

NHMA-29 - Add a "try" possibility to unsaved-value for SaveOrUpdate with assigned Guid

Michel Van Hoof created issue - 13/Nov/09 9:46 PM

I know the question has been asked many times but it seems to make sense:

We often have to replicate data between different systems where we want to keep the original guids of the data. when wanting to do so using Nhibernate, we cannot use the SaveOrUpdate possibility since there is only the option of:

always save
Always update
save when id = null
save when id = null or "value"

So basically, if you want to use SaveOrUpdate, you cannot assign your own guid's as primary keys.

In data replication situations (for example receiving data from another application using a messagequeue) you have to first check if an object is already present and then decide to save or update.

It would be nice to have an option saying "try" which would do this for you (so query the database/persistence to see if an object with the given id already exists, if not= save, else = update)

Mapping issues with `SubclassAttribute`

I have found some issues when using SubclassAttribute with attribute mapping, for example, the base class is:

[Class(Schema = "public", Table="base_resources")]
[Discriminator(Column = "type")]
public class BaseResource {
    [Id(Name = nameof(Id), Column = "id", Type = "long", Generator = "trigger-identity")]
    public virtual long Id { get; set; }
    [Property(Name=nameof(Name), Column = "name", Type = "string", Length = 32, NotNull = true)]
    public virtual string Name { get; set; }
    [Property(Name=nameof(Type), Column = "type", Type = "string", Length = 64, NotNull = true, Insert = false, Update = false)]
    public virtual string Type { get; set; }
}

The generated xml mapping is correct :

<class table="base_resources" schema="public" name="NHibernate.Extensions.UnitTest.TestDb.BaseResource, NHibernate.Extensions.UnitTest">
  <id name="Id" column="id" type="long" generator="trigger-identity" />
  <discriminator column="type" />
  <property name="Name" type="string" column="name" length="32" not-null="true" />
  <property name="Type" type="string" column="type" length="64" not-null="true" update="false" insert="false" />
 </class>

And I have a sub class which is:

[Subclass(DiscriminatorValue = "data_api", ExtendsType = typeof(BaseResource), Lazy = true)]
public class DataApi : BaseResource {
    [Join(Schema = "public", Table = "data_apis", Fetch = JoinFetch.Select)]
    [Key(Column = "id")]
    [Property(Name = nameof(Statement), Column = "statement", Length = 128, NotNull = true)]
    public virtual string Statement { get; set; }
    [Property(Name = nameof(Parameters), Column = "parameters", Length = 128, NotNull = true)]
    public virtual string Parameters { get; set; }
}

This looks correct, but the xml mapping generated is not correct:

<subclass discriminator-value="data_api" extends="NHibernate.Extensions.UnitTest.TestDb.BaseResource, NHibernate.Extensions.UnitTest" lazy="true" name="NHibernate.Extensions.UnitTest.TestDb.DataApi, NHibernate.Extensions.UnitTest">
  <property name="Parameters" column="parameters" length="128" not-null="true" />
  <join table="data_apis" schema="public" fetch="select">
    <key column="id" />
    <property name="Statement" column="statement" length="128" not-null="true" />
  </join>
</subclass>

The property element for Parameters goes out of the join element, which should be inside the join element.

Then I change the code for DataApi, move all of the PropertyAttribute to one property, like this:

[Subclass(DiscriminatorValue = "data_apis", ExtendsType = typeof(BaseResource), Lazy = true)]
public class DataApi : BaseResource {
    [Join(Schema = "public", Table = "data_apis", Fetch = JoinFetch.Select)]
    [Key(Column = "id")]
    [Property(Name = nameof(Statement), Column = "statement", Length = 128, NotNull = true)]
    [Property(Name = nameof(Parameters), Column = "parameters", Length = 128, NotNull = true)]
    public virtual string Statement { get; set; }
    public virtual string Parameters { get; set; }
}

Then the xml mapping generated is corrected now, looks like:

<subclass discriminator-value="data_api" extends="NHibernate.Extensions.UnitTest.TestDb.BaseResource, NHibernate.Extensions.UnitTest" lazy="true" name="NHibernate.Extensions.UnitTest.TestDb.DataApi, NHibernate.Extensions.UnitTest">
  <join table="data_apis" schema="public" fetch="select">
    <key column="id" />
    <property name="Parameters" column="parameters" length="128" not-null="true" />
    <property name="Statement" column="statement" length="128" not-null="true" />
  </join>
</subclass>

But is the sub class DataApi has a many-to-one mapping like this:

[Subclass(DiscriminatorValue = "data_api", ExtendsType = typeof(BaseResource), Lazy = true)]
public class DataApi : BaseResource {
    [Join(Schema = "public", Table = "data_apis", Fetch = JoinFetch.Select)]
    [Key(Column = "id")]
    [ManyToOne(Name = "DataSource", Column = "data_source_id", ClassType = typeof(DataSource), NotFound = NotFoundMode.Ignore, Lazy = Laziness.Proxy, Fetch = FetchMode.Select)]
    [Property(Name = nameof(Statement), Column = "statement", Length = 128, NotNull = true)]
    [Property(Name = nameof(Parameters), Column = "parameters", Length = 128, NotNull = true)]
    public virtual DataSource DataSource { get; set; }
    public virtual string Statement { get; set; }
    public virtual string Parameters { get; set; }
}

The xml mapping generated is not correct again, like this:

<subclass discriminator-value="data_api" extends="NHibernate.Extensions.UnitTest.TestDb.BaseResource, NHibernate.Extensions.UnitTest" lazy="true" name="NHibernate.Extensions.UnitTest.TestDb.DataApi, NHibernate.Extensions.UnitTest">
  <join table="data_apis" schema="public" fetch="select">
    <key column="id" />
    <many-to-one name="DataSource" class="Beginor.GisHub.DataServices.Data.DataSource, Beginor.GisHub.DataServices" column="data_source_id" fetch="select" lazy="proxy" not-found="ignore" />
  </join>
</subclass>

There is only one many-to-one element , other properties is not generated.

But if I move the ManyToOne after Property, like this:

[Subclass(DiscriminatorValue = "data_api", ExtendsType = typeof(BaseResource), Lazy = true)]
public class DataApi : BaseResource {
    public virtual DataSource DataSource { get; set; }
    [Join(Schema = "public", Table = "data_apis", Fetch = JoinFetch.Select)]
    [Key(Column = "id")]
    [Property(Name = nameof(Statement), Column = "statement", Length = 128, NotNull = true)]
    [Property(Name = nameof(Parameters), Column = "parameters", Length = 128, NotNull = true)]
    [ManyToOne(Name = "DataSource", Column = "data_source_id", ClassType = typeof(DataSource), NotFound = NotFoundMode.Ignore, Lazy = Laziness.Proxy, Fetch = FetchMode.Select)]
    public virtual string Statement { get; set; }
    public virtual string Parameters { get; set; }
}

Then the xml mapping generated is correct again, like this:

<subclass discriminator-value="data_api" extends="NHibernate.Extensions.UnitTest.TestDb.BaseResource, NHibernate.Extensions.UnitTest" lazy="true" name="NHibernate.Extensions.UnitTest.TestDb.DataApi, NHibernate.Extensions.UnitTest">
  <join table="data_apis" schema="public" fetch="select">
    <key column="id" />
    <property name="Parameters" column="parameters" length="128" not-null="true" />
    <property name="Statement" column="statement" length="128" not-null="true" />
    <many-to-one name="DataSource" class="Beginor.GisHub.DataServices.Data.DataSource, Beginor.GisHub.DataServices" column="data_source_id" fetch="select" lazy="proxy" not-found="ignore" />
  </join>
</subclass>

I think that's very strange behavior,is there any who can tell me the magic?

And I think SubclassAttribute should be used the same way as ClassAttribute, like this:

[Subclass(DiscriminatorValue = "data_api", ExtendsType = typeof(BaseResource), Lazy = true)]
[Join(Schema = "public", Table = "data_apis", Fetch = JoinFetch.Select)]
[Key(Column = "id")]
public class DataApi : BaseResource {
    [ManyToOne(Name = "DataSource", Column = "data_source_id", ClassType = typeof(DataSource), NotFound = NotFoundMode.Ignore, Lazy = Laziness.Proxy, Fetch = FetchMode.Select)]
    public virtual DataSource DataSource { get; set; }
    [Property(Name = nameof(Statement), Column = "statement", Length = 128, NotNull = true)]
    public virtual string Statement { get; set; }
    [Property(Name = nameof(Parameters), Column = "parameters", Length = 128, NotNull = true)]
    public virtual string Parameters { get; set; }
}

Is anyone who can improve it?

Support NetStandard 2.0

It's very exiting to see significant progress being made on porting NHibernate core to NetStandard 2.0! Is there something in the works to also port NHibernate.Mapping.Attributes? We use this library in all our NHibernate based projects.

@fredericDelaporte, is this something you're targeting in the 5.0 release?

If someone isn't already taking up the task, I'm more than happy to take this on.

NHMA-33 - Generator type "sequence" under Oracle 9i: No way to provide sequence name ==> ORA-02289: Sequence does not exist

frisco created issue - 10/Feb/11 1:59 PM

No way found to provide sequence name. My code is simple:

[Class]
public class AirPlane
{
    [Id(0, Name = "Id", TypeType = typeof(long))]
    [Generator(1, Class = "sequence")]
    public virtual long Id { get; set; }

.... 

This raise an exception when trying to persiste AirPlane with ORA-02289: Sequence does not exist (wich is normal because no way to provide sequencename which is, in my example named "airplaneseq".

Rgds.

Frico.

NHMA-7 - NHibernate.Mapping.Attributes - User interfaces

Pierre Henri Kuaté created issue - 24/Jun/06 10:24 PM

It would be interesting to add a NAnt task and maybe a Console/Windows application to generate mapping information from any assembly and save it in a file/directory.
This feature would make it possible to not use NHMA at runtime (which is a good thing because it makes the initialization of NHibernate a little bit faster).

System.Windows.Forms breaks .NET 6 project

Hello there!

Unfortunately there is a System.Windows.Forms reference in the library, this reference doesn't permit to use NHibernate engine. The exception is the following

Message "Unable to load one or more of the requested types.\r\nCould not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)" string

Is there some solution? I built a Web Api in .NET 6, .netstandard2.0 to bridge .NET 4.8 where there is the NHibernate engine. Everything works, except the load of the library.

Thanks

NHMA-28 - Annotated NHibernate entity classes can not be processed by Silverlight RIA services.

Sergey Udovenko created issue - 12/Nov/09 8:20 PM

Silverligh RIA Services internal code generator fails on attempt to process annotated NHibernate entities.

It appears that RIA Services code generator tries to read annotation attributes before the annotation class is properly initialized.

Here is a typical error spot from ClassAttribute.cs (there are many more of them):

public virtual System.Type NameType
{
	get
	{
		return System.Type.GetType( this.Name );
	}
        ...
}

The GetType() method throws a NPE because the Name is null.

A simple fix that worked for me was just to check the name for null value before calling the GetType:

return this.Name != null? System.Type.GetType( this.Name ) : null;

NHMA-5 - Add an InlineComponentAttribute to NHMA

Pierre Henri Kuaté created issue - 13/Jun/07 5:25 AM

This feature is discussed here: http://www.manning-sandbox.com/thread.jspa?threadID=18958
Basically, the idea is to be able to map a component like a collection.


Anton added a comment - 11/Jul/07 7:17 PM

Is there a chance to map one component in several classes, but column names of component members in each class are different:

1st class:

<component class="ClassifierPrimaryKey" name="_RouteType" access ="field">
  <property name="id_base" column="`id_base_routetype`" type="System.Nullable`1[System.Int32]" access="field"></property>
  <property name="id_item" column="`id_item_routetype`" type="System.Nullable`1[System.Int32]" access="field"></property>
  <property name="version" column="`version_routetype`" type="System.Nullable`1[System.Int32]" access="field"></property>
</component>

2nd class:

<component class="ClassifierPrimaryKey" name="_CostType" access ="field">
  <property name="id_base" column="`id_base_costtype`" type="System.Nullable`1[System.Int32]" access="field"></property>
  <property name="id_item" column="`id_item_costtype`" type="System.Nullable`1[System.Int32]" access="field"></property>
  <property name="version" column="`version_costtype`" type="System.Nullable`1[System.Int32]" access="field"></property>
</component>

Pierre Henri Kuaté added a comment - 12/Jul/07 11:38 AM

I already thought about something like that; but the current implementation of NHMA makes it difficult.


Anton added a comment - 12/Jul/07 4:48 PM

So, it would be very useful...
We cannot make attribute mappings with current limitations

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.