我在 hibernate 中尝试 Is-A 关系的基本示例,但出现错误。
我创建了 3 个 Employee 类的子类
- SE员工
- 员工
- 一名员工
并使用 (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>
员工.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>```
测试.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
最佳答案
此错误表明数据库中不存在表Employee。
Caused by: org.hibernate.AssertionFailure: Table hibernatedata.employee
not found
你需要更换
<property name="hbm2ddl.auto">update</property>
与
<property name="hbm2ddl.auto">create</property>
关于java - is-hibernate 中的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57883018/