gpt4 book ai didi

java - Hibernate 无法加载具有复合 id 的请求类

转载 作者:行者123 更新时间:2023-12-01 18:42:08 27 4
gpt4 key购买 nike

我使用 eclipse 插件中的 Hibernate 代码生成工具为 world database 生成 pojo 类、hbm 文件和 DAO 类。 。这生成了 City.java、Country.java、Countrylanguage.java 和 CountrylanguageId.java,但只有三个 hbm 文件 City.hbm.xml Country.hbm.xml 和 Countrylanguage.hbm.xml。

我已经通过addClass在配置中添加了java类文件

    protected SessionFactory getSessionFactory() {
Configuration config = new Configuration()
.configure("hibernate.cfg.xml")
.addClass(Country.class)
.addClass(City.class)
.addClass(Countrylanguage.class);

但是在尝试使用创建服务注册表时

StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

它抛出以下错误 -

Exception in thread "main" org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:107)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:128)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:77)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:52)
at org.hibernate.mapping.Component.getType(Component.java:227)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:455)
at org.hibernate.mapping.RootClass.validate(RootClass.java:268)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:343)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at dao.DefaultService.getSessionFactory(DefaultService.java:24)
at dao.DefaultService.<init>(DefaultService.java:14)
at dao.CityService.<init>(CityService.java:21)
at App.main(App.java:6)
Caused by: java.lang.reflect.InvocationTargetException
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.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:104)
... 13 more
Caused by: org.hibernate.MappingException: component class not found: CountrylanguageId
at org.hibernate.mapping.Component.getComponentClass(Component.java:177)
at org.hibernate.tuple.component.PojoComponentTuplizer.setComponentClass(PojoComponentTuplizer.java:147)
at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:37)
at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:41)
... 18 more
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [CountrylanguageId]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:133)
at org.hibernate.mapping.Component.getComponentClass(Component.java:174)
... 21 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : CountrylanguageId
at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:130)
... 22 more

我尝试添加

.addClass(CountrylanguageId.class)

但这会引发另一个错误,即缺少相应的 hbm 文件 Countrylanguage.hbm.xml。

基于复合 ID 关系生成的类是否需要 hbm 文件?即使在 hibernate 配置实例中指定了该类,为什么还会出现此错误?

最佳答案

解决了这个问题。当我使用 hibernate 工具生成 pojo 类时,我将其指定为默认包,但在子文件夹 src/main/java/entity 下生成它们。

hbm 文件已更新为将 class="..." 引用和 name="..." 引用设为实体.Countrylanguage,之后它工作正常。

还验证了使用配置为包作为实体、文件夹作为默认 src/main/java 的 hibernate 生成工具可以按预期正确输出 hbm 文件。

将此问题标记为已回答并关闭它。

关于java - Hibernate 无法加载具有复合 id 的请求类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59908350/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com