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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:48:18+00:00 2026-06-01T10:48:18+00:00

I have entity e.g. Product which aggregates other entities such as Category . Those

  • 0

I have entity e.g. Product which aggregates other entities such as Category. Those entities can also aggregate other entities and so on. Now I need to test my queries to database.

For simple CRUD I would create mock of EntityManager. But what if I have more complex query which I need to test for correct functionality. Then I probably need to persist entity (or more of them) and try to retrieve/update, whatever. I would also need to persist all entities on which my Product depends.

I don’t like such approach. What is the best way to test such queries?

Thanks for replies.


Update — example

Lets assume following entity structure
enter image description here

This structure is maintained by JPA implementation. For example Product class would look like this

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private Category category; 

    @ManyToOne
    private Entity1 something;    
}

So now if I want to test any query used in DAO I need to create Product in database, but it is dependent on Category and Entity1 and there is @ManyToOne annotation so values cannot be null. So I need to persist those entities too, but they have also dependencies.

I’m considering pre-creating entities such Category, Entity1 and Entity2 before test using SQL script or dbunit (mentioned by @chalimartines) which would save large amount of code, but I don’t know whether it is good solution. I would like to know some best practices for such testing.

  • 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-01T10:48:19+00:00Added an answer on June 1, 2026 at 10:48 am

    you can use @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) as

    @ContextConfiguration(locations={"classpath:/path/to/your/applicationContextTest.xml"})
    @RunWith( SpringJUnit4ClassRunner.class)
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
    public class YourClassTest {
    
        @Test
        public void test() {
            //your crud
        }
    
    }
    

    update

    You cant set the dependecies to null in order to avoid to persist them

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

Sidebar

Related Questions

I have an entity Product which has relationship category, which is optional, and use
In our system we have an entity Product , which might have various custom
I have a Product and Category entity in many-to-many association. I am trying to
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I have a product entity which has several classes (each product type has different
I have following entities: public class Product { [Key] public int Id{get;set;} //other properties
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
I have a Product entity that has a foreign key relationship to Manufacturer. I
Let's say there is an entity called Product, which is connected to Shop (a
Entity has a custom field based on which, some security roles should have access

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.