使用注释时加入子类的未知实体问题
我在使用带注释的连接子类时遇到异常,我不知道如何纠正它,请帮助。
异常是: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已用
注释掉了一个问题,询问是否需要它。你需要它。You have
<mapping class="B"/>
commented out with a question asking if it's needed. You need it.