使用注释时,已连接子类的未知实体问题

时间:2011-07-19 02:41:17

标签: java hibernate annotations joined-subclass

我在使用带注释的连接子类时遇到异常,我不知道如何纠正它,请帮忙。
异常是:org.hibernate.MappingException:未知实体:B
码:
A类

@Entity
@Table(name="table_a")
@Inheritance(strategy=InheritanceType.JOINED)   
public class A {
    // uses composite key
    @Id
    @Column(name="sid")
    private String sid;

    @Id
    @Column(name="uid")
    private String uid;

    .....
}


B类

@Entity
@Table(name="table_b")
public class B extends A {
    // inherited sid and uid from A
    @Id
    @Column(name="xid")
    private String xid;

    @Column(name="name")
    private String name;
    ......
}


create table_a(sid varchar, uid varchar);
create table_b(sid varchar, uid varchar, xid varchar, name varchar);


hibernate.cfg.xml中

<hibernate-configuration>
    <session-factory>
        .....
        <mapping class="A"/>
        <!-- no need to map B here, right?
        <mapping class="B"/>
        -->
        .....
    </session-factory>
</hibernate-configuration>


识别TestClass

public class HibernateTest {
    public static void main(String[] args) throws Exception {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        B temp = new B();
        temp.setSid(1);
        temp.setUid(2);
        temp.setXid(3);
        B target = session.get(B.class, temp);
        System.out.println("---------------" + target.getName());
        session.getTransaction().commit();
        session.close();
   }
}


例外:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: B

1 个答案:

答案 0 :(得分:2)

<mapping class="B"/>评论了一个询问是否需要的问题。你需要它。