gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:00:58 24 4
gpt4 key购买 nike

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


测试类

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

最佳答案

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

关于java - 使用注释时加入子类的未知实体问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6741740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com