1

In refrence to this. I am making an hibernate demo in which I want to map all the POJO classes dynamically. I found the above given reference and tried to scan my classes from the package mapping only. But it is giving some errors like:

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

when I added com.google.common_1.0.0.201004262004.jar I got this:

 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)

I am not able to ask or comment there that's why I am asking it here specially to @MartinAubele, @Jonathan, @SergeyBrunov, @ArthurRonald. Please give an example or something by which I can use google-reflections to scan my package.

Please Note: I don't want to use Spring. So please don't suggest about that. Thanks in Advance.

some code Sample:

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();

and another problem is : configuration.buildSessionFactory(); is not allowing me to use configuration.buildSessionFactory(serviceRegistry);

Community
  • 1
  • 1
yashpal bharadwaj
  • 323
  • 2
  • 6
  • 14
  • @luke sir please have a look you answered it [here](http://stackoverflow.com/questions/1413190/hibernate-mapping-package). please help me resolve this. – yashpal bharadwaj May 13 '16 at 08:07

1 Answers1

0

You need to use a valid version of the Reflections library:

reflections-0.9.10.jar

with dependencies:

com.google.guava:guava:18.0,

org.javassist:javassist:3.18.2-GA,

com.google.code.findbugs:annotations:2.0.1

A dependencies list can be found there: http://mvnrepository.com/artifact/org.reflections/reflections/0.9.10

Or just using Maven

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.10</version>
</dependency>

The library source and documentation: reflections

I have a project with tests for various libraries from answers on StackOverflow (like the link is provided by you):

hibernate-scanners-test

An example using Reflections: ReflectionsLibrary.java

A test class for all libraries: ScannersTest.java

Update

To fix

java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl

you just need a valid set of the Hibernate libraries. For an example, for Hibernate 4.3.5: libs_h4

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • I think the code proceed from reflections but stuck here: – yashpal bharadwaj May 13 '16 at 10:09
  • that is : serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); **sfactory = configuration.buildSessionFactory(serviceRegistry);** – yashpal bharadwaj May 13 '16 at 10:10
  • @yashpalbharadwaj `java.lang.NoClassDefFoundError` means that you have incorrect library versions. – v.ladynev May 13 '16 at 10:12
  • sir as I said Two problems are there: **1.** I am not able to use **new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).bu‌​ild(); ** I says **java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl** . **2.** when I use this code: **session.beginTransaction(); String query = "select c from " + name + " c"; System.out.println(query); hql = session.createQuery(query);** It returns a null list. – yashpal bharadwaj May 13 '16 at 10:22
  • these are my hibernate libraries: annotations-2.0.1.jar antlr-2.7.7.jar, commons-collections-3.2.1.jar, dom4j-1.6.1.jar, guava-18.0.jar, hibernate-commons-annotations-4.0.1.Final.jar, hibernate-core-4.0.0.Final.jar, hibernate-core-4.3.0.beta1.jar, hibernate-entitymanager-4.0.0.Final.jar, hibernate-jpa-2.0-api-1.0.1.Final.jar, jandex-1.0.3.Final.jar, javassist-3.18.2-GA.jar, jboss-logging-3.1.0.CR2.jar, jboss-transaction-api_1.1_spec-1.0.0.Final.jar, mysql-connector-java-5.1.37-bin.jar, ojdbc14.jar, postgresql-9.4.1208.jre7.jar, reflections-0.9.10.jar. I am using multiple DBs. – yashpal bharadwaj May 13 '16 at 10:24
  • @yashpalbharadwaj I update. And, please, use Maven or Gradle. You will not have such problems. – v.ladynev May 13 '16 at 10:48
  • I have asked [this](http://stackoverflow.com/questions/37211621/google-reflections-when-should-reflections-library-be-used-and-how-to-create) question in reference to this question please have a look sir. – yashpal bharadwaj May 13 '16 at 13:48