JPA ManyToMany question link

I have three tables. AvailableOptions and PlanTypeRef with a ManyToMany association table called AvailOptionPlanTypeAssoc. Cropped diagrams look like this:

CREATE TABLE [dbo].[AvailableOptions](
  [SourceApplication] [char](8) NOT NULL,
  [OptionId] [int] IDENTITY(1,1) NOT NULL,
  ...
)

CREATE TABLE [dbo].[AvailOptionPlanTypeAssoc](
  [SourceApplication] [char](8) NOT NULL,
  [OptionId] [int] NOT NULL,
  [PlanTypeCd] [char](2) NOT NULL,
)

CREATE TABLE [dbo].[PlanTypeRef](
  [PlanTypeCd] [char](2) NOT NULL,
  [PlanTypeDesc] [varchar](32) NOT NULL,
)

      

And the Java code looks like this.

//AvailableOption.java
@ManyToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(
  name = "AvailOptionPlanTypeAssoc",
  joinColumns = { @JoinColumn(name = "OptionId"),
                  @JoinColumn(name="SourceApplication")},
  inverseJoinColumns=@JoinColumn(name="PlanTypeCd"))
List<PlanType> planTypes = new ArrayList<PlanType>();

//PlanType.java
@ManyToMany
@JoinTable(
  name = "AvailOptionPlanTypeAssoc",
  joinColumns = { @JoinColumn(name = "PlanTypeCd")},
  inverseJoinColumns={@JoinColumn(name="OptionId"),
                      @JoinColumn(name="SourceApplication")})
List<AvailableOption> options = new ArrayList<AvailableOption>();

      

The problem arises when choosing on AvailableOptions, which it attaches to itself. Take a look at the following SQL code from backtrace. The second inner join must be on PlanTypeRef.

SELECT t0.OptionId, 
  t0.SourceApplication, 
  t2.PlanTypeCd, 
  t2.EffectiveDate, 
  t2.PlanTypeDesc, 
  t2.SysLstTrxDtm, 
  t2.SysLstUpdtUserId, 
  t2.TermDate 
FROM dbo.AvailableOptions t0 
  INNER JOIN dbo.AvailOptionPlanTypeAssoc t1 
    ON t0.OptionId = t1.OptionId AND t0.SourceApplication = t1.SourceApplication 
  INNER JOIN dbo.AvailableOptions t2 
    ON t1.PlanTypeCd = t2.PlanTypeCd 
WHERE (t0.SourceApplication = ? AND t0.OptionType = ?) 
ORDER BY t0.OptionId ASC, t0.SourceApplication ASC 
[params=(String) testApp, (String) junit0]}

      

+2


a source to share


1 answer


You are mapping a bi-directional relationship. This means that you need to choose one side as the owner of the association. This party will be responsible for updating the relationship in the database.

If you have selected AvailableOption as the owner of the relationship and you need a new PlanType to do so, you must add that setup type to the option. Adding the option only to the plant type will have no effect.

Here's the mapping:



//AvailableOption.java
@ManyToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(
  name = "AvailOptionPlanTypeAssoc",
  joinColumns = { @JoinColumn(name = "OptionId"),
                  @JoinColumn(name="SourceApplication")},
  inverseJoinColumns=@JoinColumn(name="PlanTypeCd"))
List<PlanType> planTypes = new ArrayList<PlanType>();

//PlanType.java
@ManyToMany(
    mappedBy = "planTypes"
)
List<AvailableOption> options = new ArrayList<AvailableOption>();

      

You can also refer to the documentation on hibernation annotation chapter 2.2.5

Relationship David

+3


a source







All Articles