I have some problems with hibernate. I am sharing my code.
The error I am getting
"C:\Program Files\Java\jdk1.8.0_161\bin\java" -Dfile.encoding=windows-1250 -jar C:\Users\kubci\IdeaProjects\FinalProject\target\hibernate-1.0-SNAPSHOT-jar-with-dependencies.jar maj 29, 2018 7:41:56 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {[WORKING]} maj 29, 2018 7:41:56 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found maj 29, 2018 7:41:57 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.3.Final} maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/loginpanel] maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false maj 29, 2018 7:41:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections INFO: HHH000115: Hibernate connection pool size: 20 (min=1) maj 29, 2018 7:41:58 PM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Exception in thread "main" org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [pl.Final.Project.entities.LoginConnection] at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:354) at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.(AnnotationMetadataSourceProcessorImpl.java:105) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.(MetadataBuildingProcess.java:156) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:150) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) at pl.Final.Project.Main.main(Main.java:19) Caused by: java.lang.ClassNotFoundException: Could not load requested class : pl.Final.Project.entities.LoginConnection at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:342) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:351) ... 9 more
This is my Main .
package pl.Final.Project;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import pl.Final.Project.entities.LoginUser;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Main {
private static SessionFactory factory;
public static void main(String[] args) {
factory = new org.hibernate.cfg.Configuration().configure().buildSessionFactory();
System.exit(0);
}
public static ArrayList<LoginUser> getUsersList(){
Session session = factory.openSession();
Transaction tx = null;
ArrayList<LoginUser> usersArrayList= new ArrayList<>();
try{
tx = session.beginTransaction();
List customerList = session.createQuery("FROM pl.Final.Project.entities.LoginUser WHERE id=1").list();
for(Iterator iterator = customerList.iterator(); iterator.hasNext();){
LoginUser c =(LoginUser) iterator.next();
usersArrayList.add(c);
}
tx.commit();
}catch (HibernateException e){
if(tx!=null){
tx.rollback();
}
}finally {
session.close();
}
return usersArrayList;
}
}
and cfg.xml and hmb.xml files
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/loginpanel</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="pl.Final.Project.entities.LoginConnection" />
</session-factory>
</hibernate-configuration>
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "pl.Final.Project.entities.LoginUser" table = "tuser">
<meta attribute = "class-description">
Login Customer mapping
</meta>
<id name = "id" type = "int" column = "id">
<generator class="native"/>
</id>
<property name = "login" column = "login" type = "string"/>
<property name = "password" column = "password" type = "string"/>
<property name = "name" column = "name" type = "string"/>
<property name = "surname" column = "surname" type = "string"/>
</class>
</hibernate-mapping>
I don't know what to do. I had another project with this code and it works. I did copy and paste, change some details like path etc. There are some errors and I think here is the problem:
Exception in thread "main" org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [pl.Final.Project.entities.LoginConnection]
LoginConnection.class. Figure out why the class is not in the jar and you should be good to go. - The Head Rush