【问题标题】:Hibernate join produces sql with empty "ON" clauseHibernate join 生成带有空“ON”子句的 sql
【发布时间】:2017-04-21 22:28:09
【问题描述】:

我无法使用 hibernate HQL 对两个表进行简单连接:

Query query =em.createQuery("select t from Ulist t inner join UlistTp tp");

我得到 org.hibernate.exception.SQLGrammarException:

Hibernate: select ulist0_.id as id1_2_, ulist0_.ACTUAL as ACTUAL2_2_, ulist0_.CD as CD3_2_, ulist0_.DT1 as DT4_2_, ulist0_.DT2 as DT5_2_, ulist0_.NAME as NAME6_2_, ulist0_.S1 as S7_2_, ulist0_.FK_LISTTP as FK_LISTTP8_2_ from EXS.U_LIST ulist0_ inner join EXS.U_LISTTP ulisttp1_ on
10:32:46.562 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ORA-00936: missing expression

看了之后发现“ON”子句是空的!为什么? 我认为,我的实体映射得很好:

@Entity
@Table(name = "U_LIST", schema="EXS")
public class Ulist implements java.io.Serializable  {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_EXS")
    @SequenceGenerator(name="SEQ_EXS", sequenceName="EXS.SEQ_U_LIST", allocationSize=1) 
    @Column(name = "id", unique=true, updatable = false, nullable = false)
    private Integer id;

    @Column(name = "CD", updatable = true, nullable = true)
    private String cd;

    @Column(name = "NAME", updatable = true, nullable = true)
    private String name;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="FK_LISTTP", referencedColumnName="ID")
    private UlistTp ulistTp; 
    ...getters
    ...setters
}

@Entity
@Table(name = "U_LISTTP", schema="EXS")
public class UlistTp implements java.io.Serializable  {

    public UlistTp() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_EXS")
    @SequenceGenerator(name="SEQ_EXS", sequenceName="EXS.SEQ_U_LISTTP", allocationSize=1)   
    @Column(name = "id", unique=true, updatable = false, nullable = false)
    private Integer id;

    @Column(name = "CD", updatable = true, nullable = true)
    private String cd;

    @Column(name = "NAME", updatable = true, nullable = true)
    private String name;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name="FK_LISTTP", referencedColumnName="ID")
    @Fetch(FetchMode.SUBSELECT)
    private List<Ulist> ulist = new ArrayList<Ulist>(0);

    ...getters
    ...setters

}

我使用: spring-framework 4.2.5.RELEASE

休眠 5.1.0.Final

甲骨文 11G

【问题讨论】:

    标签: java hibernate orm hql jpql


    【解决方案1】:

    试试这个

    Query query =em.createQuery("select t from Ulist t inner join t.ulistTp tp");

    【讨论】:

    • 现在可以了,但是请您解释一下您的答案
    • 你需要引用 Ulist 中定义为属性的对象。由于您引用了 UlistTp 的直接类,Hibernate 无法识别并进行相同的连接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多