我的Hibernate版本是4.3
我的职业生涯具有复合主键CareerId,并且在使用休眠工具reveng.xml生成时,会生成职业生涯和职业CareerId类,但仅会生成Career.hbm.xml,而不会对CareerId.hbm.xml进行操作。
在数据库中,仅存在Career表,而没有CareerId表。
从堆栈跟踪中,
起因:org.hibernate.MappingException:找不到组件类:CareerId
这是否意味着错误是由于未生成CarrerId.hbm.xml?
我的Hibernate.config.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!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.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.password">!QAZ2wsx</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:65064;databaseName=Testing</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.search.autoregister_listeners">true</property>
<property name="hibernate.validator.apply_to_ddl">false</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="Staff.hbm.xml" />
<mapping resource="Career.hbm.xml" />
</session-factory>
</hibernate-configuration>
我的Career.hbm.xml如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2019?~7??14?? ?W??02:43:26 by Hibernate Tools 4.3.5.Final -->
<hibernate-mapping>
<class name="Career" table="Career" schema="dbo" catalog="Testing" optimistic-lock="version">
<composite-id name="id" class="CareerId">
<key-property name="staffNo" type="serializable">
<column name="Staff_No" />
</key-property>
<key-property name="dateToRank" type="timestamp">
<column name="Date_To_Rank" length="23" />
</key-property>
<key-property name="rankAbbr" type="serializable">
<column name="Rank_Abbr" />
</key-property>
</composite-id>
<property name="title" type="serializable">
<column name="title" />
</property>
<property name="createdBy" type="serializable">
<column name="Created_By" />
</property>
<property name="createdDate" type="timestamp">
<column name="Created_Date" length="23" />
</property>
</class>
</hibernate-mapping>
我的staff.hbm.xml如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2019?~7??14?? ?W??02:43:26 by Hibernate Tools 4.3.5.Final -->
<hibernate-mapping>
<class name="Staff" table="Staff" schema="dbo" catalog="Testing" optimistic-lock="version">
<id name="staffNo" type="serializable">
<column name="StaffNo" />
<generator class="assigned" />
</id>
<property name="name" type="serializable">
<column name="Name" />
</property>
<property name="createdDate" type="timestamp">
<column name="CreatedDate" length="23" />
</property>
<property name="createdBy" type="serializable">
<column name="CreatedBy" />
</property>
</class>
</hibernate-mapping>
整个错误消息是
Exception in thread "main" java.lang.ExceptionInInitializerError
at output.Test1.main(Test1.java:17)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:101)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:122)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:81)
at org.hibernate.mapping.Component.getType(Component.java:180)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:322)
at org.hibernate.mapping.RootClass.validate(RootClass.java:271)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
at output.HibernateUtil.buildSessionFactory(HibernateUtil.java:38)
at output.HibernateUtil.<clinit>(HibernateUtil.java:19)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:98)
... 10 more
Caused by: org.hibernate.MappingException: component class not found: CareerId
at org.hibernate.mapping.Component.getComponentClass(Component.java:141)
at org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:155)
at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:64)
at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:59)
... 15 more
Caused by: java.lang.ClassNotFoundException: CareerId
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:193)
at org.hibernate.mapping.Component.getComponentClass(Component.java:138)
... 18 more
答案 0 :(得分:0)
通过引用该帖子 Hibernate-Component class not found
问题解决了。
该解决方案需要在hbm.xml中的类名称之前添加程序包名称,如下所示:
<class name="output.Career" table="Career" schema="dbo" catalog="Testing" optimistic-lock="version">
<composite-id name="id" class="output.CareerId">