Skip to content
Advertisement

I can’t extract data from the table and make a connection with the hibernate orm

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 class="ru.todolist.javafx.hibernate.TodoEntity"/>
      <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:jdk11binjava.exe --module-path D:openjFX18lib --add-modules=javafx.controls,javafx.fxml "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2022.2.2libidea_rt.jar=51993:C:Program FilesJetBrainsIntelliJ IDEA 2022.2.2bin" -Dfile.encoding=UTF-8 -classpath C:UsersdenIdeaProjectsTodoListFxTodoListFxtargetclasses;D:openjFX18libjavafx-swt.jar;D:openjFX18libjavafx.web.jar;D:openjFX18libjavafx.base.jar;D:openjFX18libjavafx.fxml.jar;D:openjFX18libjavafx.media.jar;D:openjFX18libjavafx.swing.jar;D:openjFX18libjavafx.controls.jar;D:openjFX18libjavafx.graphics.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibclassmate-1.5.1.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibhibernate-commons-annotations-5.1.0.Final.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibdom4j-2.1.1.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibFastInfoset-1.2.15.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibantlr-2.7.7.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibistack-commons-runtime-3.0.7.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjandex-2.1.1.Final.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjavax.activation-api-1.2.0.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibbyte-buddy-1.10.7.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjavassist-3.24.0-GA.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjavax.persistence-api-2.2.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjaxb-api-2.3.1.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjboss-logging-3.3.2.Final.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibjaxb-runtime-2.3.1.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibstax-ex-1.8.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibtxw2-2.3.1.jar;C:UsersdenIdeaProjectsTodoListFxTodoListFxlibhibernate-core-5.4.11.Final.jar;C:Usersden.m2repositoryorghibernatehibernate-core5.4.11.Finalhibernate-core-5.4.11.Final.jar;C:Usersden.m2repositoryorgjbossloggingjboss-logging3.3.2.Finaljboss-logging-3.3.2.Final.jar;C:Usersden.m2repositoryjavaxpersistencejavax.persistence-api2.2javax.persistence-api-2.2.jar;C:Usersden.m2repositoryorgjavassistjavassist3.24.0-GAjavassist-3.24.0-GA.jar;C:Usersden.m2repositorynetbytebuddybyte-buddy1.10.7byte-buddy-1.10.7.jar;C:Usersden.m2repositoryantlrantlr2.7.7antlr-2.7.7.jar;C:Usersden.m2repositoryorgjbossspecjavaxtransactionjboss-transaction-api_1.2_spec1.1.1.Finaljboss-transaction-api_1.2_spec-1.1.1.Final.jar;C:Usersden.m2repositoryorgjbossjandex2.1.1.Finaljandex-2.1.1.Final.jar;C:Usersden.m2repositorycomfasterxmlclassmate1.5.1classmate-1.5.1.jar;C:Usersden.m2repositoryjavaxactivationjavax.activation-api1.2.0javax.activation-api-1.2.0.jar;C:Usersden.m2repositoryorgdom4jdom4j2.1.1dom4j-2.1.1.jar;C:Usersden.m2repositoryorghibernatecommonhibernate-commons-annotations5.1.0.Finalhibernate-commons-annotations-5.1.0.Final.jar;C:Usersden.m2repositoryjavaxxmlbindjaxb-api2.3.1jaxb-api-2.3.1.jar;C:Usersden.m2repositoryorgglassfishjaxbjaxb-runtime2.3.1jaxb-runtime-2.3.1.jar;C:Usersden.m2repositoryorgglassfishjaxbtxw22.3.1txw2-2.3.1.jar;C:Usersden.m2repositorycomsunistackistack-commons-runtime3.0.7istack-commons-runtime-3.0.7.jar;C:Usersden.m2repositoryorgjvnetstaxexstax-ex1.8stax-ex-1.8.jar;C:Usersden.m2repositorycomsunxmlfastinfosetFastInfoset1.2.15FastInfoset-1.2.15.jar;C:Usersden.m2repositoryorgxerialsqlite-jdbc3.39.3.0sqlite-jdbc-3.39.3.0.jar;C:Usersden.m2repositoryorgopenjfxjavafx-controls17.0.2javafx-controls-17.0.2.jar;C:Usersden.m2repositoryorgopenjfxjavafx-controls17.0.2javafx-controls-17.0.2-win.jar;C:Usersden.m2repositoryorgopenjfxjavafx-graphics17.0.2javafx-graphics-17.0.2.jar;C:Usersden.m2repositoryorgopenjfxjavafx-graphics17.0.2javafx-graphics-17.0.2-win.jar;C:Usersden.m2repositoryorgopenjfxjavafx-base17.0.2javafx-base-17.0.2.jar;C:Usersden.m2repositoryorgopenjfxjavafx-base17.0.2javafx-base-17.0.2-win.jar;C:Usersden.m2repositoryorgopenjfxjavafx-fxml17.0.2javafx-fxml-17.0.2.jar;C:Usersden.m2repositoryorgopenjfxjavafx-fxml17.0.2javafx-fxml-17.0.2-win.jar;C:Usersden.m2repositoryorgcontrolsfxcontrolsfx11.1.1controlsfx-11.1.1.jar;C:Usersden.m2repositoryorgkordampikonliikonli-javafx12.3.1ikonli-javafx-12.3.1.jar;C:Usersden.m2repositoryorgkordampikonliikonli-core12.3.1ikonli-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

Advertisement

Answer

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 class="ru.todolist.javafx.objects.Task"/>
<!--      <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>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement