mehdi akbarian mehdi akbarian - 4 months ago 25
Java Question

Hibernate NoSuchMethodError for addPackage method

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

Answer

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

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

If you want to scan packages

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