[错误] ORA-00918:列模糊定义

时间:2013-01-28 17:37:57

标签: hibernate java-ee jpa orm createquery

有两个实体:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...
}

@Entity
public class Man extends Person {
    ...
}

尝试执行以下方法:

@Test
public void clear(EntityManager em) {
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    System.out.println(em
            .createQuery("delete from Man m where m.id > 14582647")
            .executeUpdate());
    tx.commit();
}

获取错误:

[ERROR] ORA-00918: column ambiguously defined

hibernate.show_sql显示sql-queries:

Hibernate: insert into HT_Man select man0_.id as id from Man man0_ inner join Person man0_1_ on man0_.id=man0_1_.id where id=14582647
Hibernate: delete from HT_Man

错误发生在,其中id = 14582647 ,因为必须有 id ,例如 man0_.id 。如何生成正确的sql-query?

1 个答案:

答案 0 :(得分:1)

在Man类上添加@PrimaryKeyJoinColumn注释:

@Entity
@PrimaryKeyJoinColumn(name = "<pk column name in Man table>")
public class Man extends Person {
    ...
}

另外,你应该在Person中定义一个Discriminator列,在Man中定义@DiscriminatorValue,这样Hibernate就可以找出要加入的表(当你引入一个表的女人表时)。