【问题标题】:Hibernate: referencedColumnName in JoinColumn being ignoredHibernate:JoinColumn 中的referencedColumnName 被忽略
【发布时间】:2013-06-22 04:19:38
【问题描述】:

我有两个表 FOO(包含对 BAR 中条目的引用)和 BAR 映射如下:

Foo.class:

@Id
@Column(name="ID", unique=true, nullable=false)
public BigDecimal getId() {
    return id;
}

@Column(name="BARREF", unique=true, nullable=false)
public String getBarRef() {
    return this.barRef;
}

@OneToOne(mappedBy="foo") //Inverse of the relationship
public Foo getFoo() {
    return this.foo;
}

... + other columns (including other mapped classes) ...

Bar.class:

@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="REF", updatable=false, insertable=false, referencedColumnName="BARREF")   //Owner of the relationship
public Foo getFoo() {
    return this.foo;
}

@Column(name="NAME")
public String getName() {
    return this.name;
}

... + other columns ...

执行 HQL 时:

select bar.name from Foo foo left outer join Bar bar

生成的sql是

select bar.NAME from FOO foo left outer join BAR bar on foo.ID = bar.REF

我正在寻找 SQL 的位置:

select bar.NAME from FOO foo left outer join BAR bar on foo.BARREF = bar.REF

referencedColumnName 似乎被忽略是否有原因(因此正在使用默认 PK)。是否必须引用 ID 列?

这是唯一不按 ID 列映射的表,我尝试将 ID 和 BARREF 作为 ID 列,但出现错误: '列数错误。应该是 2'

由于 Criteria 的限制,我必须为此查询使用 HQL,并且无法更改数据库架构。

【问题讨论】:

  • 您找到解决此问题的方法了吗?

标签: hibernate join mapping joincolumn


【解决方案1】:

尝试使用您想要的 sql 查询作为代码的 hql 查询

【讨论】:

  • 不幸的是我得到了unexpected token: on。如果我将 SQL 稍微更改为select bar.NAME from FOO foo left outer join BAR bar with foo.ID = bar.REF,我会得到with-clause referenced two different from-clause elements
猜你喜欢
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 2016-06-05
相关资源
最近更新 更多