Home > Enterprise >  I can't extract data from the table and make a connection with the hibernate orm
I can't extract data from the table and make a connection with the hibernate orm

Time:10-03

I start the server, I can't fill out the list in any way, the database is connected and working, table work, I can't extract data from the table and make a connection with the hibernate orm

And if I try to add writes: Caused by: org.hibernate.MappingException: Unknown entity: ru.todolist.javafx.objects.Task

class TaskHibernateImpl

    public class TaskHibernateImpl implements TaskDao {
    ....
    public ObservableList<Task> tasks = FXCollections.observableArrayList();

        @SuppressWarnings("unchecked")
        @Override
        public ObservableList<Task> findAll() {
            System.out.println("tasks.isEmpty() = "   tasks.isEmpty());
    
            tasks.clear();
            Session session = getSessionFactory().openSession();
            tasks.addAll(session.createCriteria(Task.class).list()); //here it is not filled in
            System.out.println("tasks.isEmpty() = "   tasks.isEmpty()); // true list empty
            session.close();
            return tasks;
        }
...
}

class Task object model

 [![enter image description here][1]][1]

File hibernate.cfg.xml hibernate configuration file

    <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
<!--    <property name="connection.url">jdbc:sqlite:C:/Users/den/IdeaProjects/TodoListFx/TodoListFx/db/todolist.db-->
    <property name="connection.url">jdbc:sqlite:C:/Users/den/IdeaProjects/TodoListFx/TodoListFx/db/todolist.db</property>
    <property name="connection.driver_class">org.sqlite.JDBC</property>
    <property name="hibernate.show_sql">true</property>
    <property name="dialect">ru.todolist.javafx.hibernate.dialect.SQLiteDialect</property>
    <property name="hibernate.connection.autocommit">true</property>
      <mapping />
      <mapping resource="TodoEntity.hbm.xml"/>
      <!-- <property name="connection.username"/> -->
    <!-- <property name="connection.password"/> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

TodoEntity.hbm.xml

     <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

    <class name="ru.todolist.javafx.hibernate.TodoEntity" table="todo" schema="main">
        <id name="id">
            <column name="id" sql-type="integer"/>
        </id>
        <property name="task">
            <column name="task" sql-type="text"/>
        </property>
        <property name="taskCreateTime">
            <column name="task_create_time" sql-type="text"/>
        </property>
        <property name="status">
            <column name="status" sql-type="text" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>

table todo work

enter image description here

the todolistFx program itself,see empty

enter image description here

program structure

enter image description here

logs run

    D:\jdk11\bin\java.exe --module-path D:\openjFX18\lib --add-modules=javafx.controls,javafx.fxml "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2.2\lib\idea_rt.jar=51993:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\target\classes;D:\openjFX18\lib\javafx-swt.jar;D:\openjFX18\lib\javafx.web.jar;D:\openjFX18\lib\javafx.base.jar;D:\openjFX18\lib\javafx.fxml.jar;D:\openjFX18\lib\javafx.media.jar;D:\openjFX18\lib\javafx.swing.jar;D:\openjFX18\lib\javafx.controls.jar;D:\openjFX18\lib\javafx.graphics.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\classmate-1.5.1.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\hibernate-commons-annotations-5.1.0.Final.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\dom4j-2.1.1.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\FastInfoset-1.2.15.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\antlr-2.7.7.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\istack-commons-runtime-3.0.7.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\jandex-2.1.1.Final.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\javax.activation-api-1.2.0.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\byte-buddy-1.10.7.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\javassist-3.24.0-GA.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\javax.persistence-api-2.2.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\jaxb-api-2.3.1.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\jboss-logging-3.3.2.Final.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\jaxb-runtime-2.3.1.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\stax-ex-1.8.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\txw2-2.3.1.jar;C:\Users\den\IdeaProjects\TodoListFx\TodoListFx\lib\hibernate-core-5.4.11.Final.jar;C:\Users\den\.m2\repository\org\hibernate\hibernate-core\5.4.11.Final\hibernate-core-5.4.11.Final.jar;C:\Users\den\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\den\.m2\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;C:\Users\den\.m2\repository\org\javassist\javassist\3.24.0-GA\javassist-3.24.0-GA.jar;C:\Users\den\.m2\repository\net\bytebuddy\byte-buddy\1.10.7\byte-buddy-1.10.7.jar;C:\Users\den\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\den\.m2\repository\org\jboss\spec\javax\transaction\jboss-transaction-api_1.2_spec\1.1.1.Final\jboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:\Users\den\.m2\repository\org\jboss\jandex\2.1.1.Final\jandex-2.1.1.Final.jar;C:\Users\den\.m2\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;C:\Users\den\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;C:\Users\den\.m2\repository\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;C:\Users\den\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.1.0.Final\hibernate-commons-annotations-5.1.0.Final.jar;C:\Users\den\.m2\repository\javax\xml\bind\jaxb-api\2.3.1\jaxb-api-2.3.1.jar;C:\Users\den\.m2\repository\org\glassfish\jaxb\jaxb-runtime\2.3.1\jaxb-runtime-2.3.1.jar;C:\Users\den\.m2\repository\org\glassfish\jaxb\txw2\2.3.1\txw2-2.3.1.jar;C:\Users\den\.m2\repository\com\sun\istack\istack-commons-runtime\3.0.7\istack-commons-runtime-3.0.7.jar;C:\Users\den\.m2\repository\org\jvnet\staxex\stax-ex\1.8\stax-ex-1.8.jar;C:\Users\den\.m2\repository\com\sun\xml\fastinfoset\FastInfoset\1.2.15\FastInfoset-1.2.15.jar;C:\Users\den\.m2\repository\org\xerial\sqlite-jdbc\3.39.3.0\sqlite-jdbc-3.39.3.0.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-controls\17.0.2\javafx-controls-17.0.2.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-controls\17.0.2\javafx-controls-17.0.2-win.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-graphics\17.0.2\javafx-graphics-17.0.2.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-graphics\17.0.2\javafx-graphics-17.0.2-win.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-base\17.0.2\javafx-base-17.0.2.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-base\17.0.2\javafx-base-17.0.2-win.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-fxml\17.0.2\javafx-fxml-17.0.2.jar;C:\Users\den\.m2\repository\org\openjfx\javafx-fxml\17.0.2\javafx-fxml-17.0.2-win.jar;C:\Users\den\.m2\repository\org\controlsfx\controlsfx\11.1.1\controlsfx-11.1.1.jar;C:\Users\den\.m2\repository\org\kordamp\ikonli\ikonli-javafx\12.3.1\ikonli-javafx-12.3.1.jar;C:\Users\den\.m2\repository\org\kordamp\ikonli\ikonli-core\12.3.1\ikonli-core-12.3.1.jar ru.todolist.javafx.start.Main
сент. 30, 2022 7:17:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.11.Final}
сент. 30, 2022 7:17:31 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
сент. 30, 2022 7:17:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
сент. 30, 2022 7:17:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.sqlite.JDBC] at URL [jdbc:sqlite:C:/Users/den/IdeaProjects/TodoListFx/TodoListFx/db/todolist.db]
сент. 30, 2022 7:17:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {autocommit=true}
сент. 30, 2022 7:17:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: true
сент. 30, 2022 7:17:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
сент. 30, 2022 7:17:31 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: ru.todolist.javafx.hibernate.dialect.SQLiteDialect
сент. 30, 2022 7:17:32 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
сент. 30, 2022 7:17:32 PM org.hibernate.internal.SessionImpl createCriteria
WARN: HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.sqlite.JDBC] at URL [jdbc:sqlite:C:/Users/den/IdeaProjects/TodoListFx/TodoListFx/db/todolist.db]
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {autocommit=true}
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: true
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
сент. 30, 2022 7:17:32 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: ru.todolist.javafx.hibernate.dialect.SQLiteDialect
сент. 30, 2022 7:17:32 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.sqlite.JDBC] at URL [jdbc:sqlite:C:/Users/den/IdeaProjects/TodoListFx/TodoListFx/db/todolist.db]
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {autocommit=true}
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: true
сент. 30, 2022 7:17:32 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
сент. 30, 2022 7:17:32 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: ru.todolist.javafx.hibernate.dialect.SQLiteDialect
сент. 30, 2022 7:17:32 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
сент. 30, 2022 7:17:32 PM org.hibernate.internal.SessionImpl createCriteria
WARN: HHH90000022: Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead

CodePudding user response:

I fill in my ObservableList , list , which I got from the table

as you can see the todolist program has filled up enter image description here

class TaskHibernateImpl in this class, data is taken from the table ,method tasksclear() checking the table clearing so that there is no double table filling, we clear the current one.createQuery("From Task") here I attention! specify my class entity

    public class TaskHibernateImpl implements TaskDao { 
    ...
    public ObservableList<Task> findAll() {
                tasks.clear(); // 
                tasks.addAll(HibernateSessionFactoryUtil.getSessionFactory().openSession().createQuery("From Task").list());
                System.out.println("tasks.isEmpty() = "   tasks.isEmpty()); // false 
                return tasks;
            }
...
}

class HibernateSessionFactoryUtil -> In this class, I create a configuration and add a class to it, with a JPA configuration

public class HibernateSessionFactoryUtil {

    private static SessionFactory sessionFactory;

    private static ServiceRegistry serviceRegistry;

    public HibernateSessionFactoryUtil() {
    }

    public static SessionFactory getSessionFactory(){
        if(sessionFactory == null){
            try{
                Configuration configuration = new Configuration().configure();
                // here necessary class add
                configuration.addAnnotatedClass(ru.todolist.javafx.objects.Task.class);
                StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                sessionFactory = configuration.buildSessionFactory(builder.build());
            } catch (Exception e) {
                System.out.println("Exception !"   e);
            }
        }
        return sessionFactory;
    }
}

class Task -> In this class I put JPA annotations @Entity, @Id, @Column

package ru.todolist.javafx.objects;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "todo", schema = "main", catalog = "")
public class Task {

    private SimpleIntegerProperty id = new SimpleIntegerProperty();
    private SimpleStringProperty task = new SimpleStringProperty("");

    private static Date date = new Date();
    private SimpleStringProperty time = new SimpleStringProperty(date.toString());

//    private static boolean statusBegin = false;
    private static String statusText = "не выполнена";
    private SimpleStringProperty status = new SimpleStringProperty(statusText);

    public Task() {
    }

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id", nullable = false)
    public int getId() {
        return id.get();
    }

    public SimpleIntegerProperty idProperty() {
        return id;
    }

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

    @Basic
    @Column(name = "task", nullable = false, length = -1)
    public String getTask() {
        return task.get();
    }

    public SimpleStringProperty taskProperty() {
        return task;
    }

    public void setTask(String task) {
        this.task.set(task);
    }

    @Basic
    @Column(name = "task_create_time", nullable = false, length = -1)
    public String getTime() {
        return time.get();
    }

    public SimpleStringProperty timeProperty() {
        return time;
    }

    public void setTime(String time) {
        this.time.set(time);
    }

    @Basic
    @Column(name = "status", nullable = true, length = -1)
    public String getStatus() {
        return status.get();
    }

    public SimpleStringProperty statusProperty() {
        return status;
    }

    public void setStatus(String status) {
        this.status.set(status);
    }

    @Override
    public String toString() {
        return "Task{"  
                "id="   id  
                ", task="   task  
                ", time="   time  
                ", status="   status  
                '}';
    }

}

class hibernate.cfg.xml hibernate configuration class

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.url">jdbc:sqlite:C:/Users/den/IdeaProjects/TodoListFx/TodoListFx/db/todolist.db</property>
    <property name="connection.driver_class">org.sqlite.JDBC</property>
    <property name="hibernate.show_sql">true</property>
    <property name="dialect">ru.todolist.javafx.hibernate.dialect.SQLiteDialect</property>
    <property name="hibernate.connection.autocommit">true</property>

      <mapping />
<!--      <mapping resource="Task.hbm.xml"/>-->
      <!-- <property name="connection.username"/> -->
    <!-- <property name="connection.password"/> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>
  • Related