Home > Software engineering >  Java Hibernate exception
Java Hibernate exception

Time:08-18

For some reason it does not find Polica even tho i have mapped it in code, created seperate file to map it etc. What is the problem?

In file PolicaDAO2 i am also getting createQuery deprecated for some reason if anyone knows solution to this aswell let me know. I tried multiple things aswell as you can see in the file under findall() function

Exception:

Aug 09, 2022 12:41:34 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.1.2.Final
Aug 09, 2022 12:41:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
Aug 09, 2022 12:41:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver
Aug 09, 2022 12:41:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:mysql://127.0.0.1:3306/Wallboard]
Aug 09, 2022 12:41:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=root}
Aug 09, 2022 12:41:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Aug 09, 2022 12:41:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
Aug 09, 2022 12:41:43 PM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Aug 09, 2022 12:41:47 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Aug 09, 2022 12:41:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
Aug 09, 2022 12:41:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: com.mysql.cj.jdbc.Driver
Aug 09, 2022 12:41:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:mysql://127.0.0.1:3306/Wallboard]
Aug 09, 2022 12:41:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=root}
Aug 09, 2022 12:41:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Aug 09, 2022 12:41:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
Aug 09, 2022 12:41:48 PM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Aug 09, 2022 12:41:48 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.query.sqm.UnknownEntityException: Could not resolve root entity 'Polica'
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:175)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:182)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:761)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:663)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:127)
    at digiwallboard.DAO.PolicaDAO2.findAll(PolicaDAO2.java:81)
    at digiwallboard.Service.PolicaService.findAll(PolicaService.java:18)
    at Main.main(Main.java:27)
Caused by: org.hibernate.query.sqm.UnknownEntityException: Could not resolve root entity 'Polica'
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitRootEntity(SemanticQueryBuilder.java:1628)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitRootEntity(SemanticQueryBuilder.java:243)
    at org.hibernate.grammars.hql.HqlParser$RootEntityContext.accept(HqlParser.java:1874)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitEntityWithJoins(SemanticQueryBuilder.java:1548)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitFromClause(SemanticQueryBuilder.java:1539)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuery(SemanticQueryBuilder.java:833)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:629)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitQuerySpecExpression(SemanticQueryBuilder.java:243)
    at org.hibernate.grammars.hql.HqlParser$QuerySpecExpressionContext.accept(HqlParser.java:1218)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:623)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSimpleQueryGroup(SemanticQueryBuilder.java:243)
    at org.hibernate.grammars.hql.HqlParser$SimpleQueryGroupContext.accept(HqlParser.java:1131)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitSelectStatement(SemanticQueryBuilder.java:399)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitStatement(SemanticQueryBuilder.java:358)
    at org.hibernate.query.hql.internal.SemanticQueryBuilder.buildSemanticModel(SemanticQueryBuilder.java:285)
    at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:81)
    at org.hibernate.internal.AbstractSharedSessionContract.lambda$createQuery$2(AbstractSharedSessionContract.java:748)
    at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:141)
    at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:128)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:745)
    ... 5 more

Main.java

import digiwallboard.Util.HibernateUtil;
import digiwallboard.Entity.*;

import java.util.List;

import digiwallboard.DAO.*;
import digiwallboard.Service.PolicaService;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        PolicaService policaService = new PolicaService();
        
        
        List<Polica> pol = policaService.findAll();
        
        for(Polica p : pol) {
            System.out.println("-"   p.toString());
        }
    }

}

PolicaDAO2.java

package digiwallboard.DAO;

import java.util.List;

import javax.persistence.TypedQuery;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import digiwallboard.Entity.Polica;

public class PolicaDAO2 implements PolicaDaoInterface<Polica, String> {
    
    private Session currentSession;
    
    private Transaction currentTransaction;
 
    public PolicaDAO2() {
        
    }
 
    public Session openCurrentSession() {
        currentSession = getSessionFactory().openSession();
        return currentSession;
    }
 
    public Session openCurrentSessionwithTransaction() {
        currentSession = getSessionFactory().openSession();
        currentTransaction = currentSession.beginTransaction();
        return currentSession;
    }
     
    public void closeCurrentSession() {
        currentSession.close();
    }
     
    public void closeCurrentSessionwithTransaction() {
        currentTransaction.commit();
        currentSession.close();
    }
     
    private static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        configuration.addAnnotatedClass(Polica.class);
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
        return sessionFactory;
    }
 
    public Session getCurrentSession() {
        return currentSession;
    }
 
    public void setCurrentSession(Session currentSession) {
        this.currentSession = currentSession;
    }
 
    public Transaction getCurrentTransaction() {
        return currentTransaction;
    }
 
    public void setCurrentTransaction(Transaction currentTransaction) {
        this.currentTransaction = currentTransaction;
    }
 
    @SuppressWarnings("deprecation")
    public void persist(Polica entity) {
        getCurrentSession().save(entity);
    }
  
    @SuppressWarnings({ "unchecked", "deprecation" })
    public List<Polica> findAll() {
        
        Session session = openCurrentSession();
        
        TypedQuery<Polica> result = (TypedQuery<Polica>) openCurrentSession().createQuery("FROM Polica");
        List<Polica> result = query.getResultList();
        
        closeCurrentSession();
        /*
        Query query = getCurrentSession().createQuery("select * from Polica");
        
        List<Polica> pol = (List<Polica>) getCurrentSession().createNativeQuery("from Polica").list();
        return pol;
        
        */
        return result;
        
        
        /*
        TypedQuery<Polica> query = createQuery( "select * from Polica");
               
        return query.getResultList();
        */
    }
     

}

PolicaDaoInterface.java

package digiwallboard.DAO;

import java.io.Serializable;
import java.util.List;

public interface PolicaDaoInterface<T, Id extends Serializable> {
    
    public List<T> findAll();

}

Polica.java

EDIT: IF YOU ARE USING HIBERNATE 6.0 DO NOT USE JAVAX.PERSISTENCE LIKE I DID HERE THIS IS ONE OF THE REASONS I WAS GETTING THE EXCEPTION INSTEAD USE JAKARTA.PERSISTENCE

package digiwallboard.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "Polica")
public class Polica {
    
    @Id
    @Column(name="id")
    private int id;
    @Column(name="ime_pol")
    private String ime_pol;
    

    public Polica() {
        
    }

    public Polica(int id, String ime_pol) {
        super();
        this.id = id;
        this.ime_pol = ime_pol;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getIme_pol() {
        return ime_pol;
    }


    public void setIme_pol(String ime_pol) {
        this.ime_pol = ime_pol;
    }

    @Override
    public String toString() {
        return "Polica [id="   id   ", ime_pol="   ime_pol   "]";
    }

    
}

PolicaService.java

package digiwallboard.Service;

import java.util.List;

import digiwallboard.DAO.PolicaDAO2;
import digiwallboard.Entity.Polica;

public class PolicaService {

    private static PolicaDAO2 policaDao;
    
    public PolicaService() {
        policaDao = new PolicaDAO2();
    }
    
    public List<Polica> findAll() {
        policaDao.openCurrentSession();
        List<Polica> police = policaDao.findAll();
        policaDao.closeCurrentSession();
        return police;
    }
    
}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">Root123</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/Wallboard</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <!--  <mapping  resource="springfoxdemo/java/swagger/Polica.hbm.xml"/> -->
        <mapping />
    </session-factory>
</hibernate-configuration>

hibernate.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

<class name="digiwallboard.Entity.Polica" table="Polica">
        <id name="id" type="int" column="id">
            <column name="id" />
            
        </id>
        <property name="ime_pol" column="ime_pol"
            type="String">
            
        </property>
        
 </class>

</hibernate-mapping>

CodePudding user response:

First of all, to avoid deprecation warnings, you should use the type safe variant of createQuery:

TypedQuery<Polica> result = openCurrentSession().createQuery("FROM Polica", Polica.class);

It's hard to say why your code is not working, but I have a few ideas what you could try. Note that you don't reference the hibernate.hbm.xml file anywhere and you commented out the reference to some other file Polica.hbm.xml, though the contents are unnecessary anyway as the annotations on the entity class are enough.

You are correctly listing the class through <mapping />, so it should be picked up by Hibernate. My guess is, that either some of your hbm.xml files that you might reference in your real app are interfering somehow, i.e. registering the entity type under a different name, or that your configuration is not picked up for some reason.

You can check which entities Hibernate found by inspecting getSessionFactory().getMetamodel().getEntities(), and if you find your entity there, check what EntityType#getName() reports, as that is the under which you can use the entity in HQL.

  • Related