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 195969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:43:39+00:00 2026-05-11T16:43:39+00:00

I have a many-to-one relationship between objects Product and Supplier . I need to

  • 0

I have a many-to-one relationship between objects Product and Supplier. I need to be able to delete Supplier without deleting the Products that belong to it.

Here is a simplified version of classes:

public class Supplier {
    public virtual IList<Product> Products { get; protected set; }
}

public class Product {
    // Product belongs to a Category but Supplier is optional
    public virtual Supplier Supplier { get; set; }
    public virtual Category Category { get; set; }
}

I’m using FluentNHibernate, but here are the mappings it produces:

<bag name="Products" cascade="save-update" inverse="true">
      <key column="SupplierID" />
      <one-to-many class="Me.Product, Me, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>

<many-to-one name="Supplier" column="SupplierID" />

This creates a foreign key on the Products table, so when I attempt to do a straight delete on a Supplier I get a foreign key constraint error. I tried changing cascade to ‘all’, in the hope that it might only delete the relationship, but it deleted all Products and their other associated objects.

The only way I can see to solve this right now is to iterate the Products collection of Supplier and set the Supplier property to null. Is there a way I can achieve this behaviour through the mapping?

  • 1 1 Answer
  • 1 View
  • 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-05-11T16:43:39+00:00Added an answer on May 11, 2026 at 4:43 pm

    The mapping properties only take effect when the entity is actually loaded, and when you are not querying through HQL. As an example, if you specify Cascade=ALL, if you delete a supplier with the query "delete from Supplier where id=:id", you will probably get the same FK constraint failure, because the hql doesn’t trigger (programmatic) cascades.

    It seems that the Products are the owning side of the relation, which is good. I think you have two choices:

    • Code some method on the Supplier to iterate through all Products and set the Product’s supplier to null, and use this method before deleting any Suppliers
    • Before issuing a Supplier delete, make sure your DAO sets the product’s supplier to null

    Example:

    public int Delete(Supplier s) {
        return Session.CreateQuery("udpate Product set Supplier = null where Supplier = :supplier")
            .SetParameter("supplier", s)
            .ExecuteUpdate();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one to many relationship between two objects, lets call them Gallery
I have one-to-many relationship between parent and child Java objects. The parent object uses
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
I have a one-to-many relationship between two classes for this situation. I have a
I have a one-to-many relationship between Foo and Bar and I cannot perform an
I have a many-to-one relationship that users can edit via checkboxes. PK of Foo
I have a Type MenuItem that has one-to-many relationship with itself through Children property.
I have many-to-one mappings working fine, but a one-to-many relationship between locations and location_times
I'm having some difficulty creating nested objects between a one-to-many relationship. My models are
I've got two entities with a one-to-many relationship between them. The entity that holds

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.