开发者

JPA ManyToMany referencing issue

开发者 https://www.devze.com 2022-12-27 04:24 出处:网络
I have three tables.AvailableOptions and PlanTypeRef with a ManyToMany association table called AvailOptionPlanTypeAssoc.The trimmed down schemas look like this

I have three tables. AvailableOptions and PlanTypeRef with a ManyToMany association table called AvailOptionPlanTypeAssoc. The trimmed down schemas 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 making a select on AvailableOptions it joins back onto itself. Note the following SQL code from the backtrace. The second inner join should 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.AvailOptionPlanTyp开发者_运维百科eAssoc 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]}


You are mapping a bidirectional association. That means you have to choose one side as the owner of the association. This side will be responsible for updating the relationship in the database.

If you choose AvailableOption as the owner of the relationship and you want a new PlanType for it, you have to add the plantype to the option. Adding the option only to the plantype will have no effect.

Here is 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 may also refer to the hibernate annotation documentation chapter 2.2.5

Regards David

0

精彩评论

暂无评论...
验证码 换一张
取 消