1

有两个实体:

@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 查询:

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 查询?

4

1 回答 1

1

在 Man 类上添加 @PrimaryKeyJoinColumn 注释:

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

此外,您应该在 Person 中定义一个 Discriminator 列,在 Man 中定义 @DiscriminatorValue ,以便 Hibernate 可以确定要加入哪些表(当您介绍一个 Woman 表时)。

于 2013-01-28T18:01:16.637 回答