对此。_ 我正在制作一个休眠演示,我想在其中动态映射所有 POJO 类。我找到了上面给出的参考,并尝试仅从包映射中扫描我的类。但它给出了一些错误,例如:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Predicate
at com.telemune.util.StringUtil.getSessionFactory(StringUtil.java:44)
at com.telemune.generator.TableDescriber.describeTable(TableDescriber.java:33)
at com.telemune.generator.PojoGenerator.main(PojoGenerator.java:169)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Predicate
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 3 more
当我添加com.google.common_1.0.0.201004262004.jar我得到了这个:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableSet;
at org.reflections.Reflections.getTypesAnnotatedWith(Reflections.java:358)
at com.telemune.util.StringUtil.getSessionFactory(StringUtil.java:46)
at com.telemune.generator.TableDescriber.describeTable(TableDescriber.java:33)
at com.telemune.generator.PojoGenerator.main(PojoGenerator.java:169)
我无法在那里提问或发表评论,这就是为什么我在这里专门向@MartinAubele、@Jonathan、@SergeyBrunov、@ArthurRonald 提问。请举例说明我可以使用 google-reflections 扫描我的包裹。
请注意:我不想使用 Spring。所以请不要提出这个建议。提前致谢。
一些代码示例:
SessionFactory sfactory = null;
ServiceRegistry serviceRegistry=null;
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
Reflections reflections = new Reflections(PojoGenerator.pkgName);
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);
for(Class<?> clazz : classes)
{
configuration.addAnnotatedClass(clazz);
}
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sfactory = configuration.buildSessionFactory();
另一个问题是: configuration.buildSessionFactory(); 不允许我使用 configuration.buildSessionFactory(serviceRegistry);