參考this。我正在做一個hibernate演示,我想要動態地映射所有的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();不允許我使用 配置。buildSessionFactory(serviceRegistry);
@luke先生請看看你回答它[這裏](http://stackoverflow.com/questions/1413190/hibernate-mapping-package)。請幫我解決這個問題。 –