我正在尝试休眠状态下Is-A关系的基本示例,但会出错。
我创建了Employee Class的3个子类
并使用(joined-subclass)标签建立关系。
在此之前,我尝试使用(sub-class)标签使用相同的示例,并且工作得很好,但是现在显示了使用(joined-subclass)错误。 除了xml标记外,我什么都没有改变。 不知道我在这里做错了。
mysql.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedata</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="resources/employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Employee.hbm.xml
<joined-subclass name="beans.SEmployee" table="semployee" extends="beans.Employee">
<key column="id"></key>
<property name="tool"></property>
</joined-subclass>
<joined-subclass name="beans.HEmployee" table="hemployee" extends="beans.Employee">
<key column="id"></key>
<property name="wh"></property>
</joined-subclass>
<joined-subclass name="beans.AEmployee" table="aemployee" extends="beans.Employee">
<key column="id"></key>
<property name="server"></property>
</joined-subclass>
</class>
</hibernate-mapping>```
Test.java
public static void main(String[] args)
{
Configuration cfg=new Configuration();
cfg.configure("resources/mysql.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction t=session.beginTransaction();
SEmployee se=new SEmployee(100,"abc","abc@gmail.com",2000,"hibernate");
HEmployee he=new HEmployee(200,"abc","abc@gmail.com",2000,20);
AEmployee ae=new AEmployee(300,"abc","abc@gmail.com",2000,"oracle");
session.save(se);
session.save(he);
session.save(ae);
t.commit();
session.close();
sf.close();
}
异常日志:
Sep 11, 2019 10:25:58 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.4.Final}
Sep 11, 2019 10:25:59 AM
org.hibernate.annotations.common.reflection.java.JavaReflectionManage
<clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for
production use!)
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL
[jdbc:mysql://localhost:3306/hibernatedata]
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Sep 11, 2019 10:26:01 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
Sep 11, 2019 10:26:01 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Sep 11, 2019 10:26:03 AM org.hibernate.AssertionFailure <init>
ERROR: HHH000099: an assertion failure occurred (this may indicate a
bug in Hibernate, but is more likely due to unsafe use of the
session): org.hibernate.AssertionFailure: Table hibernatedata.employee
not found
Sep 11, 2019 10:26:03 AM
org.hibernate.engine.jdbc.connections.internal.
DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool
[jdbc:mysql://localhost:3306/hibernatedata]
Exception in thread "main" org.hibernate.MappingException: Could not
instantiate persister
org.hibernate.persister.entity.JoinedSubclassEntityPersister
at
org.hibernate.persister.internal.PersisterFactoryImpl.
createEntityPersister(PersisterFactoryImpl.java:112)
at
org.hibernate.persister.internal.PersisterFactoryImpl.
createEntityPersister(PersisterFactoryImpl.java:77)
at
org.hibernate.metamodel.internal.MetamodelImpl.
initialize(MetamodelImpl.java:181)
at org.hibernate.internal.SessionFactoryImpl.<init>
(SessionFactoryImpl.java:300)
at
org.hibernate.boot.internal.
SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
at
org.hibernate.cfg.Configuration.
buildSessionFactory(Configuration.java:708)
at
org.hibernate.cfg.Configuration.
buildSessionFactory(Configuration.java:724)
at test.Client_Relations.main(Client_Relations.java:17)
Caused by: org.hibernate.AssertionFailure: Table hibernatedata.employee
not found
at
org.hibernate.persister.entity.AbstractEntityPersister.
getTableId(AbstractEntityPersister.java:5541)
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>
(JoinedSubclassEntityPersister.java:450)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance
(NativeConstructorAccessorImpl.java:62)
at
sun.reflect.DelegatingConstructorAccessorImpl.
newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.hibernate.persister.internal.PersisterFactoryImpl.
createEntityPersister(PersisterFactoryImpl.java:96)
... 7 more
答案 0 :(得分:1)
此错误表明表 Employee 在数据库中不存在。
Caused by: org.hibernate.AssertionFailure: Table hibernatedata.employee
not found
您需要替换
<property name="hbm2ddl.auto">update</property>
使用
<property name="hbm2ddl.auto">create</property>