Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8762063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:26:06+00:00 2026-06-13T15:26:06+00:00

Given the code and mapping below, Why am I getting this error? Error from

  • 0

Given the code and mapping below, Why am I getting this error?

Error from Failing test

    [Test]
    public void Cascade_Can_Persist_Hero_Without_Epic()
    {
        _epic = new Epic("illiad");
        var hero = new GreekHero("ted");
        hero.SetEpic(_epic);
        _session.SaveOrUpdate(_epic);
        _session.Flush();
        _session.Evict(_epic);
        Assert.That(_epic.IsPersistent());
        Assert.That(hero.IsPersistent());

        var found = _session.Get<GreekHero>(hero.Id);
        found.Look();

        Assert.That(found, Is.EqualTo(hero));
    }

NHibernate: select next_hi from hibernate_unique_key
NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 6 [Type: Int32 (0)], @p1 = 5 [Type: Int32 (0)]
NHibernate: select next_hi from hibernate_unique_key
NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 7 [Type: Int32 (0)], @p1 = 6 [Type: Int32 (0)]
NHibernate: 
    INSERT INTO Epic (EpicName, EpicId) 
    VALUES (@p0, @p1);
        @p0 = 'illiad' [Type: String (0)], 
        @p1 = 163840 [Type: Int32 (0)]
NHibernate: 
    INSERT INTO GreekHero (HeroName, EpicId, GreekHeroId) 
    VALUES (@p0, @p1, @p2);
        @p0 = 'ted' [Type: String (0)], 
        @p1 = 163840 [Type: Int32 (0)], 
        @p2 = 196608 [Type: Int32 (0)]
Test 'Core.Data.Tests.NHibernate.Bootstraps.MappingTests.CollectionMappingConstraintTests.Cascade_Can_Persist_Hero_Without_Epic' failed:
    NHibernate.Exceptions.GenericADOException : could not insert: 
    [Core.TestingSupport.GreekGods.Domain.GreekHero#196608][SQL: INSERT INTO GreekHero (HeroName, EpicId, GreekHeroId) VALUES (?, ?, ?)]
  ----> System.Data.SqlServerCe.SqlCeException : 
  A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK253126F517FE25F6 ]

Object Model (parent)

public class Epic : Entity
{
    ...

    [UsedImplicitly] // NHib uses this
    public Epic() {
        _greekHeroes = new HashedSet<GreekHero>();
    }

    /// <summary>
    /// The backing store for order items. NHib will set this when loaded from the db.
    /// </summary>
    private readonly Iesi.Collections.Generic.ISet<GreekHero> _greekHeroes;

    /// <summary>
    /// An enumeration of greek heros for public consumption.
    /// </summary>
    public virtual IEnumerable<GreekHero> GreekHeroes { get { return _greekHeroes; } }

    public virtual bool AddItem(GreekHero newItem)
    {
        if (!ReferenceEquals(newItem, null) && _greekHeroes.Add(newItem))
        {
            newItem.SetEpic(this);
            return true;
        }
        return false;
    }

    public virtual bool RemoveItem(GreekHero itemToRemove)
    {
        if (!ReferenceEquals(itemToRemove, null) && _greekHeroes.Remove(itemToRemove))
        {
            itemToRemove.SetEpic(null);
            return true;
        }
        return false;
    }
}

Object Model (child)

public class GreekHero : Entity
{
    ...

    #endregion

    #region Epic (a Greek Hero may be associated with an Epic)

    /// <summary>
    /// The Epic associated with this Greek Hero.
    /// </summary>
    public virtual Epic Epic { get; protected set; }

    /// <summary>
    /// This enforces referential integrity in the object model between this <see cref="GreekHero"/>
    /// and the <see cref="Domain.Epic.GreekHeroes"/>.
    /// </summary>
    /// <param name="epic">The new epic associated with this hero.</param>
    /// <remarks><seealso cref="Domain.Epic.AddItem"/> and <seealso cref="Domain.Epic.RemoveItem"/></remarks>
    public virtual void SetEpic(Epic epic)
    {
        var prevEpic = Epic;
        if (epic == prevEpic) return;

        Epic = epic;

        if (!ReferenceEquals(prevEpic, null))
            prevEpic.RemoveItem(this);

        if (!ReferenceEquals(epic, null))
            epic.AddItem(this);
    }

    #endregion
}

HBM Mapping (Parent)

<id name="Id">
  <column name="EpicId" />
  <generator class="hilo" />
</id>

<natural-id>
  <property name="EpicName" length="30" />
</natural-id>

<set name="GreekHeroes"
     cascade="all-delete-orphan"
     inverse="true"
     access="field.camelcase-underscore">
  <key column="GreekHeroId" />
  <one-to-many class="GreekHero"/>
</set>

HBM Mapping (child)

<id name="Id">
  <column name="GreekHeroId" />
  <generator class="hilo" />
</id>

<natural-id>
  <property name="HeroName" length="30" />
</natural-id>

<many-to-one name="Epic" column="EpicId" />

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:26:07+00:00Added an answer on June 13, 2026 at 3:26 pm

    The <key> element in <set> indicates the column in the child table where the key of the parent is to be stored. In your case it should be “EpicId” since that is what you state in the <many-to-one>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given: code inside a stored proc: select bleh into #tblTemp from FunctionThatReturnsTable('some','params') -- do
Given the code below, how would you create/implement SR.h so that it produces the
Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted
Given the code below, how do I compare a List of objects's values with
Is there a way to get the hibernate mapping from within my application code?
Consider the following code: public abstract class Base { public void getAnswer(); } public
I'm using EF 4.1 Code First, with a Fluent Mapping: Entity: public class MyClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.