使用注释时加入子类的未知实体问题

发布于 2024-11-25 02:35:48 字数 1836 浏览 0 评论 0原文

我在使用带注释的连接子类时遇到异常,我不知道如何纠正它,请帮助。
异常是: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

I met exception when using annotated joined subclass, i don't know how to correct it, please help.

Exception is: org.hibernate.MappingException: Unknown entity: B

Code:

Class 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;

    .....
}

Class 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;
    ......
}

Tables

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2024-12-02 02:35:48

您已用 注释掉了一个问题,询问是否需要它。你需要它。

You have <mapping class="B"/> commented out with a question asking if it's needed. You need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文