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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:59:51+00:00 2026-06-04T00:59:51+00:00

I have a many-to-many relationship between 2 tables that I define with a join

  • 0

I have a many-to-many relationship between 2 tables that I define with a join table.

//MyClassA
@ManyToMany
@JoinTable(name="A_TO_B", 
           joinColumns=@JoinColumn(name="A_UUID"), 
           inverseJoinColumns=@JoinColumn(name="B_UUID")
)
private List<MyClassB> classBs = new ArrayList<MyClassB>();

//MyClassB
@ManyToMany(mappedBy="classBs")
private List<MyClassA> classAs = new ArrayList<MyClassA>();

Due to company architecture restrictions, the join table must have a UUID column.

I have a DAO method called associateItems which should insert a row into the join column. In order to test everything, I do the following in a jUnit test.

myDao.associateItems(classAItem, classBItem);

classAItem = myDao.get(classAItem.getUuid());
classBItem = myDao.get(classBItem.getUuid());

assertEquals(1, classAItem.getClassBs().size());
assertEquals(1, classBItem.getClassAs().size());

This test fails on the last 2 lines.

In my DAO, I have tried a number of things. I tried adding the classAItem and classBItem to each others lists and merging them. But this causes an error because the join table object doesn’t have a UUID. And I tried creating the join table object and persisting that. But then the last 2 lines of the test fail because it thinks the size of the lists are 0.

I have been sticking with the second solution (fail is better than error).

public void associateItems(MyClassA classAItem, MyClassB classBItem) {
    //UUID populated in constructor
    AToB association = new AToB(classAItem, classBItem); 
    entityManager.persist(association);
}

I know that the join table item is getting inserted into the database. I also manually entered a join table item and made sure the lists are populated when an association is found. So the test just has an issue with making the join table item and then associating the relevant lists.

I have tried messing with cascades, fetch types, and flushes. But I can’t seem to find a way to make the tests pass.

Anyone know what I need to do to fix this?

  • 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-04T00:59:53+00:00Added an answer on June 4, 2026 at 12:59 am

    This was actually a problem with the get method in the DAO. I added the following hint and test run without failure.

    query.setHint(QueryHints.REFRESH, HintValues.TRUE);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to map one-to-many relationship between two tables that have the
I have an intermediate table that defines many-to-many relationship between, for instance, Customer and
I have a one to many relationship between two tables. The many table contains
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a many-to-many relationship between users and groups and I have a table
I have a Hash Map (many-to-one relationship between texts and boolean values): name flag
I have a many to many relationship between tables A and B. Since I
I have a problem :( I have a many-many table between two tables(1&2), via
I have the following tables in my database that have a many-to-many relationship, which
I have two tables: messages (messages users posted), likes (many to many relationship between

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.