0

I have 3 classes:

public class WorkEntity
{
    public virtual int WorkId { get; set; }
    public virtual OrderExtensionEntity OrderExtension { get; set; }
}

public class OrderExtensionEntity
{
    public virtual int Id { get; set; }
    public virtual WorkEntity Work { get; set; }
    public virtual IList<IssueInformation> Issues { get; set; }
}

public class IssueInformationEntity
{
    public virtual int Id { get; set; }
    public virtual long WorkId { get; set; }
}

They are mapped:

public class WorkMap : ClassMap<WorkEntity>
{
    public WorkMap()
    {
        Id(x => x.WorkId).Column("ID_ZLECENIE").GeneratedBy.Sequence("SEQ_TAE_ZLECENIE");
        HasOne(x => x.OrderExtension).Constrained();
     }
}

public class OrderExtensionMap : ClassMap<OrderExtensionEntity>
{
    public OrderExtensionMap()
    {
        LazyLoad();

        Id(x => x.Id).Column("ID_ZLECENIE").GeneratedBy.Foreign("Work");
        HasMany(x => x.IssueInformation).KeyColumn("FK_ZLECENIE");
    }
}

public class IssueInformationMap : ClassMap<IssueInformationEntity>
{
    public IssueInformationMap()
    {
       Id(x => x.Id).Column("ID_ZAD_ROZ_ADRES").GeneratedBy.Sequence("SEQ_ZAD_ROZ_ADRES");

        Map(x => x.WorkId).Column("FK_ZLECENIE").Not.Nullable();
    }
}

When I create records on the database (Oracle) I can get all the information from tables. But I can't save OrderExtensionEntity using:

var orderExtension = new OrderExtension();
orderExtension._some_variables_like_strings_to_be_saved_

Session.SaveOrUpdate(orderExtension)

It tells me "Unresolved property Work" and some other error if I try to do:

orderExtension.Work = new WorkEntity() { WorkId = 12345 };
Daniel G
  • 433
  • 1
  • 7
  • 19

1 Answers1

0

Try this mapping. This is how my one to one relationships look.

public class WorkMap : ClassMap<WorkEntity>
{
    public WorkMap()
    {
        Id(x => x.WorkId).Column("ID_ZLECENIE").GeneratedBy.Sequence("SEQ_TAE_ZLECENIE");
        HasOne(x => x.OrderExtension)
            .Cascade.All();
     }
}

public class OrderExtensionMap : ClassMap<OrderExtensionEntity>
{
    public OrderExtensionMap()
    {
        LazyLoad();

        Id(x => x.Id).Column("ID_ZLECENIE").GeneratedBy.Foreign("Work");
        HasOne(x => x.Work).Constrained();
        HasMany(x => x.IssueInformation).KeyColumn("FK_ZLECENIE");
    }
}
Cole W
  • 15,123
  • 6
  • 51
  • 85
  • Nope. It throws "The transaction under which this method call was executing was asynchronously aborted." Maybe I change from One-to-One to One-To-Many and use it like One? – Daniel G Dec 05 '12 at 13:42
  • Well I'm not sure I understand your example above. It looks like you are trying to save an OrderExtension by itself. An OrderExtension cannot exist without a Work object. If this is not your intention then the mapping is incorrect. I would think you would link your new OrderExtension object to either a new Work object or a persistent Work object and save the Work object. – Cole W Dec 05 '12 at 13:46
  • Also the error you state above "The transaction ..." doesn't seem like a mapping related issue. – Cole W Dec 05 '12 at 13:50
  • Saving the Work object could make it work. But for reliability I would like to store this entity separately. How can I change mapping to achieve that effect? – Daniel G Dec 05 '12 at 14:15
  • @DanielGralak If an `OrderExtensionEntity` can exist on it's own without having a Work object then this is not a one to one relationship. If the relationship between OrderExtensionEntity and Work is optional then the primary key in the OrderExtensionEntity table should be seperate and the relationship to Work should be expressed via a nullable foreign key to the Work table. This way you don't have to have a related Work object. – Cole W Dec 05 '12 at 14:21
  • OrderExtensionEntity is optional to Work, so on the table it should have e.g. ID_ORDEREXTENSION and FK_ZLECENIE (Work)? and in the class mappings how it should be referenced? – Daniel G Dec 05 '12 at 14:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20631/discussion-between-cole-w-and-daniel-gralak) – Cole W Dec 05 '12 at 15:43