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.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]}