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

  • Home
  • SEARCH
  • 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 984643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:02:53+00:00 2026-05-16T05:02:53+00:00

I have an application in which I am trying to implement ManyToMany relation between

  • 0

I have an application in which I am trying to implement ManyToMany relation between 2 entities using Hibernate as my JPA provider.

The example I am trying is an uni-directional one, wherein a Camera can have multiple Lenses and Lense can fit into multiple Cameras.

Following is my entity class…(Just pasting the relevant part of it)

Camera:

@Entity
@Table(name = "CAMERAS")
public class Camera implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "CAM_ID", nullable = false)
    private Long camId;

    @Column(name="CAMERA_BRAND")
    private String cameraBrand;

    @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE},fetch=FetchType.EAGER)
    @JoinTable(name="CAMERA_LENS",joinColumns=@JoinColumn(name="CAM_ID"),inverseJoinColumns=@JoinColumn(name="LENS_ID"))
        private Set<Lens> lenses = new HashSet<Lens>();

    ...
}

Lens:

@Entity
@Table(name = "LENSES")
public class Lens implements Serializable {        
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "LENS_ID", nullable = false)
    private Long lensId;

    @Column(name="LENS_NAME")
    private String name;

    ...
}

In the test-case, I try to persist the Camera instance like this…

@Test
public void test_saveCameraLenseManyToMany() {
    Camera cam = new Camera();
    cam.setCameraBrand("Nikon");
    Set<Camera> cameras = new HashSet<Camera>();
    cameras.add(cam);

    Set<Lens> lenses = new HashSet<Lens>();
    Lens lens1 = new Lens();
    lens1.setName("WideAngle");
    lenses.add(lens1);

    cam.setLenses(lenses);

    // concreteDAO.saveLens(lens1);
    concreteDAO.saveCamera(cam);
}

The persistence happens successfully, and there is no error/exception thrown by Hibernate. However, contrary to expectation, a row is not created in the JoinTable (CAMERA_LENS in this case). A row is created in each of Lens and Camera tables only, thereby not serving the purpose of ManyToMany. However, the DDL is generated for the join table and it is created as well, but just that it does not have any records.

Any assistance or pointers would be much appreciated.

  • 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-16T05:02:53+00:00Added an answer on May 16, 2026 at 5:02 am

    I found the problem. It is a bit weird. The DAO class that I was using had a class level annotation of:

    @Transactional(readOnly=true)
    

    The sql generated was

    Hibernate: insert into CAMERAS (CAMERA_BRAND) values (?)
    Hibernate: insert into LENSES (LENS_NAME) values (?)
    Hibernate: insert into LENSES (LENS_NAME) values (?)
    

    And still it saved the 2 entities, but did not save or insert data into the join table.

    The moment I added a method level annotation of :

    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    

    everything turned out to be fine, and data was indeed inserted into the join table. The new SQLs generated in this case was…

    Hibernate: insert into CAMERAS (CAMERA_BRAND) values (?)
    Hibernate: insert into LENSES (LENS_NAME) values (?)
    Hibernate: insert into LENSES (LENS_NAME) values (?)
    
    Hibernate: insert into CAMERAS_LENSES (CAM_ID, LENS_ID) values (?, ?)
    Hibernate: insert into CAMERAS_LENSES (CAM_ID, LENS_ID) values (?, ?)
    

    Kinda weird still that no transaction propogation strategy saved data into 2 tables, but once the propogation strategy was given, everything turned out to be fine.

    Another interesting observation is that I tried putting the propogation strategy as MANDATORY at the method level (to check if there existed a Transaction which led to the first and faulty case), however the method threw an exception indicating that no transaction existed, still the data was saved into the 2 tables.

    Hope that helps someone further on…

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

Sidebar

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.