Unknown entity issue for joined subclass when using annotation
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("-开发者_如何学Python--------------" + target.getName());
session.getTransaction().commit();
session.close();
}
}
Exception:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: B
You have <mapping class="B"/>
commented out with a question asking if it's needed. You need it.
精彩评论