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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:35:06+00:00 2026-05-12T15:35:06+00:00

We are using builder pattern to generate test data. These domain objects have relations

  • 0

We are using builder pattern to generate test data. These domain objects have relations between them. Our functional tests require these objects to be persisted.

Think about this model:

domain model

If I want a plain instance of C I do aNew().c().build()

If I want it to be persisted I do aNew().c().saveIn(session)

If I want an instance of C with a known B I do aNew().c().with(b).build()

Well, you got the idea. My problem is, if I want to persist a C, should it persist it’s B? Or should it be persisted before hand? What about if I want a reasonable default B? What about if I want to persist a D? Should it persist all A, B, C?

Of course real system is much more complex (sometimes with circular references). I am looking for a best practice for persisting complex test data.

Edit: It looks like I have bumped into the language barrier, my mother language is not English, so I am sorry for obscurity. Here is more information:

  • It is not legacy code that I am trying to test
  • I am trying to write a coverage test, NOT a unit test (as a result I won’t be mocking anything)
  • The piece of software I am trying to test works if the database is populated to some extend (it does not use all entities).

PS. Please don’t hesitate to ask for more information, because I have been struggling to find the possible best practice. The closest thing I have come up with is:

  1. Keep track of what has been set explicitly while building an entity.
  2. Assume that explicitly set entities are already persisted, do not persist them.
  3. Persist everything else (with their own persister).

This will work, but my spider sense is tingling, I think I am doing something wrong because, there will be logic involved in test code, it will be very complex to deal with without tests.

Edit 2: I will try to make myself more clear. When I am writing/running my unit and some integration tests I have no problem, because the test data are not persisted, it lives in memory.

But when I try to persist my test data, hibernate will not let me save an entity without it’s relations.

How can I overcome this problem?

  • 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-05-12T15:35:06+00:00Added an answer on May 12, 2026 at 3:35 pm

    You need to define your cascades on the domain better. If you can’t test it, how do you expect it will perform in the real application?

    For example:

    A -> B: Who’s the owner of this relationship? Do you want to add B to A, or the other way around? This can be an implementation detail where you can have both B.SetParent(A) and A.Children.Add(B), and where you set B’s parent to A in case of A.Children.Add(B) (likewise the other way around). What happens if you do:

    A a1 = new A();
    A a2 = new A();
    B b = new B();
    a1.Children.Add(b);
    b.SetParent(a);
    

    You need to make up your mind here. None of the solutions are perfect, so it’s basically personal preference and app consistency that applies here.

    Working with ORMs you get into these constraint problems faster then with plain SQL (or any other datasource like XML or your own datasource), but you’d need to consider the problems if you were to write plain SQL too.

    I’m sorry, I don’t have an definite answer for you, but to me it looks like you need to consider some constraints which (I presume) you haven’t done yet.

    Personally, I like the repository-pattern when dealing using NHibernate in DALs. I make my repositories implement from IDisposable and let them get a session each. This way you get the “Unit of work”-pattern into your design.

    Good luck with it 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 178k
  • Answers 178k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try: foreach($xml->xpath('/rss/channel/item') as $item) { print_r($item->title); // This pulls out… May 12, 2026 at 3:35 pm
  • Editorial Team
    Editorial Team added an answer <?php // Gets a list of all the 2nd-level keys… May 12, 2026 at 3:35 pm
  • Editorial Team
    Editorial Team added an answer While I did not find a program that can aid… May 12, 2026 at 3:35 pm

Related Questions

I have an old PowerBuilder application that we are slowly phasing out. We are
Here's our problem, we are a Flex shop that uses .NET for the server
We have thousands of DOS programs that were all written in the 80s using
Where I work we need to rethink the way we develop software and keep
I am very new to Flash and I need help to get started with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.