开发者

Nhibernate - How to Design DomainObjects and Mappings with CompositeId

at first i'm really new to ORM, nhibernate and FHN. i look into this stuff for a few days now. i have an existing database with 4 tables:

Nhibernate - How to Design DomainObjects and Mappings with CompositeId

so i tried first to get the Auswahl and RefAuswahlFilter to work.

here are my DomainObjects: pls let me know when i can do this better

[Serializable]
public class Auswahl
{
    public Auswahl()
    {
        this.RefFilters = new List<RefAuswahlFilter>();
    }

    public virtual IList<RefAuswahlFilter> RefFilters { get; set; }

    //...rest of the Properties

[Serializable]
public class RefAuswahlFilter
{
    public virtual Auswahl Auswahl { get; set; }
    public virtual long Filterrank { get; set开发者_JAVA百科; }
    public virtual string Filter { get; set; }

here are my mappings:

public class AuswahlMap : ClassMap<Auswahl>
{
    public AuswahlMap()
    {
        Table("AUSWAHL");

        Id(x => x.Id)
            .GeneratedBy.Sequence("SEQ_AUSWAHL");

        Map(x => x.Programm);
        Map(x => x.Variante);
        Map(x => x.Returnkey);
        Map(x => x.Beschreibung);

        HasMany<RefAuswahlFilter>(x => x.RefFilters);

    }
}

public class RefAuswahlFilterMap : ClassMap<RefAuswahlFilter>
{
    public RefAuswahlFilterMap()
    {
        Table("REFAUSWAHLFILTER");

        CompositeId()
            .KeyReference(x => x.Auswahl,"IDAUSWAHL")
            .KeyProperty(x => x.Filterrank,"FILTERANK");

        Map(x => x.Filter);
    }
}

if i run my app i got a list with all my Auswahl rows.

var l = session.CreateCriteria(typeof(Auswahl)).List<Auswahl>();

but when i look into an item to get the RefFilters the debugger show the following error:

innerException {"ORA-00904: \"REFFILTERS0\".\"AUSWAHL_ID\": ungültiger Bezeichner\n"}

could not initialize a collection: [ORMTest.DomainModel.Auswahl.RefFilters#1097][SQL: SELECT reffilters0_.Auswahl_id as Auswahl4_1_, reffilters0_.IDAUSWAHL as IDAUSWAHL1_, reffilters0_.FILTERANK as FILTERANK1_, reffilters0_.IDAUSWAHL as IDAUSWAHL3_0_, reffilters0_.FILTERANK as FILTERANK3_0_, reffilters0_.Filter as Filter3_0_ FROM REFAUSWAHLFILTER reffilters0_ WHERE reffilters0_.Auswahl_id=?]

so i think something on my mapping is wrong, but i really dont get it.

thx.


well i was partially successful. reading from the db works now.

public AuswahlMap()
    {
        Table("AUSWAHL");

        Id(x => x.Id,"ID")
            .GeneratedBy.Sequence("SEQ_AUSWAHL");

        Map(x => x.Programm);
        Map(x => x.Variante);
        Map(x => x.Returnkey).Not.Nullable();
        Map(x => x.Beschreibung);

        HasMany(x => x.RefFilters)
            .Inverse()
            .Cascade.All();    
    }

public RefAuswahlFilterMap()
    {
        Table("REFAUSWAHLFILTER");

        CompositeId()
            .KeyReference(x => x.Auswahl,"IDAUSWAHL")
            .KeyProperty(x => x.Filterrank);

        Map(x => x.Filter);

        References(x => x.Auswahl)
            .Column("IDAUSWAHL")
            .Not.Nullable();
    }

Saving to DB just works with an empty List (RefFilters) till now, so i have to look into this now. this is from my log. i really dont know where "NHibernate.Type.Int64Type - binding '1' to parameter: 3 14:31:18.585 [9] DEBUG" come from but this is the exception i get so far.

14:31:18.397 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Inserting entity: [ORMTest.DomainModel.RefAuswahlFilter#ORMTest.DomainModel.RefAuswahlFilter] 14:31:18.413 [9] DEBUG NHibernate.AdoNet.AbstractBatcher - Opened new IDbCommand, open IDbCommands: 1 14:31:18.413 [9] DEBUG NHibernate.AdoNet.AbstractBatcher - Building an IDbCommand object for the SqlString: INSERT INTO REFAUSWAHLFILTER (Filter, IDAUSWAHL, Filterrank) VALUES (?, ?, ?) 14:31:18.413 [9] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [ORMTest.DomainModel.RefAuswahlFilter#ORMTest.DomainModel.RefAuswahlFilter] 14:31:18.413 [9] DEBUG NHibernate.Type.StringType - binding 'fh' to parameter: 0 14:31:18.413 [9] DEBUG NHibernate.Type.Int64Type - binding '1446' to parameter: 1 14:31:18.413 [9] DEBUG NHibernate.Type.Int64Type - binding '1446' to parameter: 2 14:31:18.413 [9] DEBUG NHibernate.Type.Int64Type - binding '1' to parameter: 3 14:31:18.585 [9] DEBUG NHibernate.AdoNet.AbstractBatcher - Closed IDbCommand, open IDbCommands: 0 14:31:18.647 [9] DEBUG NHibernate.AdoNet.ConnectionManager - registering flush end

EDIT: if i change my mapping to

public RefAuswahlFilterMap()
    {
        Table("REFAUSWAHLFILTER");

        CompositeId()
            .KeyReference(x => x.Auswahl,"IDAUSWAHL")
            .KeyProperty(x => x.Filterrank);

        Map(x => x.Filter);
    }

saving works like expected. nevertheless the NHibernate Profiler shows some warnings and my select for RefAuswahlFilter looks not that good :)

SELECT reffilters0_.IDAUSWAHL  as IDAUSWAHL1_,
       reffilters0_.Filterrank as Filterrank1_,
       reffilters0_.IDAUSWAHL  as IDAUSWAHL3_0_,
       reffilters0_.Filterrank as Filterrank3_0_,
       reffilters0_.Filter     as Filter3_0_
FROM   REFAUSWAHLFILTER reffilters0_
WHERE  reffilters0_.IDAUSWAHL = 1 /* :p0 */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜