0

i use Hibernate for connecting to my db and i configure my database in side of my dbConnector class:

public static SessionFactory connectingHibernate(){
    Properties database=new Properties(); 

    database.setProperty("hibernate.connection.driver_class",PROPERTY_NAME_DATABASE_DRIVER);
    database.setProperty("hibernate.connection.username",USERNAME);
    database.setProperty("hibernate.connection.password",PASSWORD);
    database.setProperty("hibernate.connection.url",url);
    database.setProperty("hibernate.dialect",DIALECT);

    Configuration cfg=new Configuration()
            .setProperties(database)
            .addPackage("androidapi.model")
            .addAnnotatedClass(User.class)
            .addAnnotatedClass(Product.class)
            .addAnnotatedClass(PriceProduct.class)
            .addAnnotatedClass(CoinPrice.class)
            .addAnnotatedClass(SellPage.class)
            .addAnnotatedClass(Setting.class)
            .addAnnotatedClass(Message.class);



    StandardServiceRegistryBuilder ssrb=new StandardServiceRegistryBuilder()
            .applySettings(cfg.getProperties());
    sessionFactory=cfg.buildSessionFactory(ssrb.build());
    return sessionFactory;
}

when i run my application from inside of my ide my code works correctly but after making war file with maven i got this exception on methods that use my db! how can i fix this?

exception:

  "error": "Internal Server Error",
  "exception": "java.lang.NoSuchMethodError",
  "message": "org.hibernate.cfg.Configuration.addPackage(Ljava/lang/String;)Lorg/hibernate/cfg/Configuration;",

after removeing this line: .addPackage("androidapi.model") i got this exception:

"exception": "java.lang.NoSuchMethodError",
  "message": "org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;",
  "path": "/myinsta/admin_api/mGetAllUser"

my pom.xml Hibernate dependencies:

 <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>LATEST</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>LATEST</version>
    </dependency>

solved! read answer and its good to know that you should use compatible version of hibernate that can find here at meaven part: Maven dependency

2 Answers2

3

You have multiple versions of hibernate on your classpath (at the time of this writing this: most likely 3.6.0.beta2 and at 5.2.1.Final)

Remove:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>LATEST</version>
    <type>pom</type>
</dependency>

This is pulling in an older version.

raphaëλ
  • 6,393
  • 2
  • 29
  • 35
1

You don't need this line

addPackage("androidapi.model")

The method addPackage() doesn't scan a package. It is for reading "package-level metadata" (.package-info- files).

Looks like, you use Hibernate 5. You can't use Hibernate 4 configuration code with Hibernate 5

https://stackoverflow.com/a/32711654/3405171

If you want to scan packages

https://stackoverflow.com/a/35248061/3405171

To use a valid Hibernate version just check this page with Hibernate core versions:

https://mvnrepository.com/artifact/org.hibernate/hibernate-core

and add it to the pom.xml (if you use Maven). For example for Hibernate 5.2.1.Final (It uses Java 8):

  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>5.2.1.Final</version>
  </dependency>

A dependency from hibernate-core is pretty enough, because of all other libraries will be loaded by Maven as transitive dependences. However, to use logger you need to add an additional dependency.

If you want to download an archive with all required jars, you can check the Hibernate download page:

http://hibernate.org/orm/downloads/

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67