Support Many to Many relationships with additional fields in join table

Background - used hibernate for 18 (?) years. Just upgraded web app app from 2.5 to 5.3. …and I see that you have the @ManyToMany relationship. But it does not support additional fields in the join table. This is a major party foul )). The “Party” is RDBMS. It has been going strong since 1970 SQL. And I know how great Hibernate is for Web Based R.A.D. and generally joining the DB to HTML, especially with Spring. But in many cases, including a ginormeous Oil Company Invoicing app on hibernate, that bit about not support many to many relationships with additional fields in the join table cauese gung ho Java developers to simply denormalize the RDBMS unreal mistake. So let’s put our best foot forward and support good RDBMS design.

With that said, yes, I know the bazaaro embeddedID/IdClass Mantra as a solution for the same. But the insane complexity and having to MICRO manage your embeddedId class (e.g. @EmbeddedId private UserGroupId id = new UserGroupId():wink: is as I suggested a “party foul”. So if you agree, I am all for helping and will start with a plain vanilla user -> usergroup -> group example where the join table (usergroup) has additional column “supervisor”.

Hibernate has been supporting many-to-many-to-one associations with extra tables for a very long time. Just map the intermediary table as an entity. Check out this article for more details.

Right I started with that code 1 month ago. Between then and now, I had lots of different errors that I can not all list… …but one theme was a reflection error that resulted from an internal hibernate NPE because the embeddedId class was not instantiated unless I declared it with a “new()” as I listed in my post above. Search google for “embedded id not accessible reflection”, top result is close to this issue in my app (spring 5, hibernate 5.3, tomcat 9, jdk 10, jcache, etc…)

So, with that “party foul” and a lot of other tweaking, based on your example (and about 5 others) I finally, after a month, got it to work, but I still dont have the remove group service working (user many2many groups). So the reason I am posting here is not to fix/improve my app, it is to fix/improve hibernate many to many mapping to make the mapping and the entity manager easier, less bug prone, more intuitive, and look more like hibernate standard api rather than a one-off, almost-does-not-work solution…

Right I started with that code 1 month ago. Between then and now, I had lots of different errors that I can not all list… …but one theme was a reflection error that resulted from an internal hibernate NPE because the embeddedId class was not instantiated unless I declared it with a “new()” as I listed in my post above.

Use this test case template to replicate the issue. Currently, I have no clue what you are talking about.

So the reason I am posting here is not to fix/improve my app, it is to fix/improve hibernate many to many mapping to make the mapping and the entity manager easier, less bug prone, more intuitive, and look more like hibernate standard api rather than a one-off, almost-does-not-work solution…

Again, I have no idea what do you mean by: “make the mapping and the entity manager easier”, “less bug prone”, “more intuitive” or “look more like hibernate standard api rather than a one-off, almost-does-not-work solution”.

If Hibernate was like you described it, it would have never become the most widespread Java data access framework that is used by millions of developers every day.

So, in the true spirit of open-source software development, if you have any concrete idea of how Hibernate could be improved, you should create a Jira issue.

Will do thanks. I also feel positive about hibernate, but i was hoping for less buggy nany to many mapping (with adfizional fields in join table).

Interesting note, i just got a contract writing java/skala for a robot synchronisation system (warehousing) and the tech stack looked great for hibernate. But the owner said that they replaced hibernate with jooq (never heard if it) because of useability issues. So i would start with the npe/jira as you suggested. But down the road making the @ManyToMany mapping support additional fields in the join table sounds like a no brainer.

If you want a “less buggy nany to many mapping (with adfizional fields in join table)”, you just have to read the User Guide. It provides a solution that works like a charm since it’s based on unit tests that are executed on every CI job.

Thanks for the link to user guide. There is a section titled ManyToMany, is that it? It has a personadress many to many join table but only with the two fk’s. Using that example, I need fields like Boolean billingaddress and Boolean mailingaddress in the join table personadress. That is what I mean by “additional fields in the join table”. Does the ManyToMany annotation support such additional fields in the join table? If yes, an anchor to that section please?

Yes, that’s how many-to-many associations work.

Using that example, I need fields like Boolean billingaddress and Boolean mailingaddress in the join table personadress.

Yes, you can add those to that table and it will work if you map the link table as PersonAddress.

Does the ManyToMany annotation support such additional fields in the join table? If yes, an anchor to that section please?

The @ManyToMany annotation does not, but if you map the link table, it supports additional fields, as I already demonstrated in this article.