Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How do I insert into the bridging table for many to many tables ?
How do I insert into the bridging table for many to many tables ? [message #1769327] Sat, 29 July 2017 15:41 Go to next message
Karen Goh is currently offline Karen GohFriend
Messages: 15
Registered: February 2012
Junior Member
I am very new in JPA.

I have started off my project using Java EE servlet with DAO and JSP. But, I have problem in the know-how in inserting into the bridging table.

Please refer to my database schema. (If there is anything wrong here, kindly let me know also). Tks.

And now, I am using/adding EclipseLink with JPA with Tomcat 8 on top of the servlet layer.

All the basic like annotations like mappedby, POJO etc has been done.

My problem is how do I tell the Entity Manager to insert the data from my first table Id and 2nd table Id into the bridging table in my database - MySQL.

BTW, I am putting my EntityManager in my controller.

I will do the set of my Model attributes to the request getParameter values.

And then I will do a commit by em.persist(t) ; // t being the instance of my model.

I repeat the same for my 2nd model and now I am stuck how to go about doing the insertion for the bridging table.

Can someone please share with me some tips ?

Tks.
Re: How do I insert into the bridging table for many to many tables ? [message #1769482 is a reply to message #1769327] Tue, 01 August 2017 02:24 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1275
Registered: July 2009
Senior Member
Subject and Tutor have a @ManyToMany relationship - this is handled transparently by JPA. What is different from your xlsx table description though is that you have duplicated the subject data into the tutor_subject table. Normally this would just contain the IDs of the two entities involved.

See https://www.eclipse.org/eclipselink/api/2.0.2/javax/persistence/ManyToMany.html where your model is similar to the Customer Phone model.
Re: How do I insert into the bridging table for many to many tables ? [message #1769928 is a reply to message #1769482] Sun, 06 August 2017 06:11 Go to previous messageGo to next message
Karen Goh is currently offline Karen GohFriend
Messages: 15
Registered: February 2012
Junior Member
Yes. The conventional is using id from both tables. I just thought that it could be done this way so that it's easier for me if added in the subjectName.

Anyway, thanks for highlighting this to me. really appreciate it.
Re: How do I insert into the bridging table for many to many tables ? [message #1770178 is a reply to message #1769928] Tue, 08 August 2017 21:06 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1275
Registered: July 2009
Senior Member
Your situation is duplicating data from the originating table, which is a bad idea IMO, but putting additional fields in the relation table is a common occurrence. It depends on the data, as you can use a Map where the key or value is something that gets stored in the relation table. In most cases though, it is usually advisable to create an entity for this relation table, so you can control what goes into it directly, then map to it using 1:M and 1:M. Its primary key would be the the relationships - see the derived ID examples which allow JPA to use the relationship mappings as the primary key - https://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers for example.
Previous Topic:java.lang.NullPointerException while converting DynamicEntity
Next Topic:History Tables for Entities using Single_Table Inheritance Strategy
Goto Forum:
  


Current Time: Wed Dec 13 11:31:27 GMT 2017

Powered by FUDForum. Page generated in 0.01627 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software