org.hibernate.InvalidMappingException: Unable to read XML - java

i need help with hibernate mapping one-to-many
i don´t know why this is wrong
public class Direccion {
private long id;
private String calle;
private int numero;
private List<Persona> habitantes;
public Direccion(){
}
public Direccion(long id, String calle, int numero, List<Persona> habitante) {
super();
this.id = id;
this.calle = calle;
this.numero = numero;
this.habitantes = habitante;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCalle() {
return calle;
}
public void setCalle(String calle) {
this.calle = calle;
}
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
public List<Persona> getHabitantes() {
return habitantes;
}
public void setHabitantes(List<Persona> habitantes) {
this.habitantes = habitantes;
}
#Override
public String toString() {
return "Direccion [id=" + id + ", calle=" + calle + ", numero="
+ numero + ", habitantes=" + habitantes + "]";
}
}
another class:
public class Persona {
private long id;
private long nombre;
public Persona(){
}
public Persona(long id, long nombre) {
super();
this.id = id;
this.nombre = nombre;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getNombre() {
return nombre;
}
public void setNombre(long nombre) {
this.nombre = nombre;
}
#Override
public String toString() {
return "Persona [id=" + id + ", nombre=" + nombre + "]";
}
}
the mapping
<?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="datos.Direccion" table="direccion">
<id column="id" name="id">
<generator class="identity">
</generator>
</id>
<property column="calle" name="calle" type="string" />
<property name="numero" column="numero" type="int" />
<set name="habitantes" table="persona" inverse="true" lazy="true" fetch="select">
<key>
<column name="idPersona" not-null="true" />
</key>
<one-to-many class="datos.Persona" />
</class>
</hibernate-mapping>
another mapping:
<?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="datos.Persona" table="persona">
<id column="id" name="id">
<generator class="identity"/>
</id>
<property column="nombre" name="nombre" type="string" />
<many-to-one name="direccion" class="datos.Direccion" column="idPersona" not-null="true"/>
</set>
</class>
</hibernate-mapping>
hibernate configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/repasohibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property> <!-- en true muestra hql en consola -->
<!--Mapeo Entidades -->
<mapping resource="mapeos/persona.hbm.xml" />
<mapping resource="mapeos/direccion.hbm.xml" />
</session-factory>
</hibernate-configuration>
this is the error:
jun 21, 2014 6:11:51 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
jun 21, 2014 6:11:51 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
jun 21, 2014 6:11:51 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
jun 21, 2014 6:11:51 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: mapeos/persona.hbm.xml
jun 21, 2014 6:11:51 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
ERROR en la inicialización de la SessionFactory: org.hibernate.InvalidMappingException: Unable to read XML
Exception in thread "main" java.lang.NullPointerException
at dao.DireccionDao.traerListaRubros(DireccionDao.java:128)
at prueba.prueba.main(prueba.java:12)
if anyone can help me with that i´m so grateful
thanks!

If you are mapping one Direccion to many Personas, this is a one-to-many mapping. This also means that the Persona has a many-to-one relationship with Direccion. You have a List<Persona> variable under the Direccion class. Now you need to have a Direccion class variable for the Persona class.
Here is an example of hibernate configuration with xml. http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/

some times in Init-param and context-param you try to give same param-value that's why it is giving error.try to remove one.
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Sample Spring Maven Project</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml,
/WEB-INF/applicationContext-datasource.xml,
/WEB-INF/applicationContext-hibernate.xml</param-value>
</context-param>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
I have removed this one.
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml,
/WEB-INF/applicationContext-datasource.xml,
/WEB-INF/applicationContext-hibernate.xml</param-value>
</init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Related

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
the todolistFx program itself,see empty
program structure
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
I fill in my ObservableList , list , which I got from the table
as you can see the todolist program has filled up
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>

It is necessary to fill the ObservableList by taking data from the table using hibernate

It is necessary to fill the ObservableList by taking data from the table using hibernate, it turns out to be empty, the table is working, it turns out to be empty, the table works,
session open
class Main
public class Main {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static void main(String[] args) {
findAll();
}
#SuppressWarnings({"unchecked", "deprecation"})
public static ObservableList<Task> findAll() {
ObservableList<Task> observableTasks = FXCollections.observableArrayList();
Session session = getSessionFactory().openSession();
observableTasks.addAll(session.createCriteria(Task.class).list());
System.out.println("observableTasks = " + observableTasks.isEmpty()); // true empty
session.close();
return observableTasks;
}
public static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
file 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 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">org.example.hibernate.dialect.SQLiteDialect</property>
<property name="hibernate.connection.autocommit">true</property>
<mapping class="org.example.test_hibernate_connect_bd.Task"/>
<mapping resource="TodoTask.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>
file TodoTask.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="org.example.test_hibernate_connect_bd.Task" 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="time">
<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>
class Task the object itself
logs current launch
сент. 30, 2022 8:40:14 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.11.Final}
сент. 30, 2022 8:40:15 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
сент. 30, 2022 8:40:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
сент. 30, 2022 8:40:15 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 8:40:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {autocommit=true}
сент. 30, 2022 8:40:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: true
сент. 30, 2022 8:40:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
сент. 30, 2022 8:40:15 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.example.hibernate.dialect.SQLiteDialect
сент. 30, 2022 8:40:16 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 8:40:16 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
observableTasks = true
project structure
I fill in the list first, because the ObservableList class inherits from extends List , then it gets the same addAll(....) methods. I fill in my ObservableList , list , which I got from the table.
class Main
package org.example;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.example.objects.Task;
import org.example.utils.HibernateSessionFactoryUtil;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static ObservableList<Task> personList = FXCollections.observableArrayList();
public static List<Task> ist = new ArrayList<>();
public static void main(String[] args) {
findAll();
}
#SuppressWarnings({"unchecked", "deprecation"})
public static ObservableList<Task> findAll() {
ist = HibernateSessionFactoryUtil.getSessionFactory().openSession().createQuery("From Task").list();
personList.addAll(ist);
System.out.println("personList.isEmpty() = " + personList.isEmpty());
return personList;
}
}
class HibernateSessionFactoryUtil -> In this class, I create a configuration and add a class to it, with a JPA configuration
package org.example.utils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
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(org.example.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 org.example.objects;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javax.persistence.*;
#Entity
#Table(name = "todo", schema = "main", catalog = "")
public class Task {
private SimpleIntegerProperty id = new SimpleIntegerProperty();
private SimpleStringProperty task = new SimpleStringProperty("");
private SimpleStringProperty time = new SimpleStringProperty("");
private SimpleStringProperty status = new SimpleStringProperty("");
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 +
'}';
}
}
the result I wanted to see is that the list is not empty
окт. 01, 2022 8:33:53 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.11.Final}
окт. 01, 2022 8:33:54 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
окт. 01, 2022 8:33:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
окт. 01, 2022 8:33:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.sqlite.JDBC] at URL [jdbc:sqlite:C:/Users/den/IdeaProjects/TestHibernate/db/todolist.db]
окт. 01, 2022 8:33:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {autocommit=true}
окт. 01, 2022 8:33:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: true
окт. 01, 2022 8:33:54 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
окт. 01, 2022 8:33:55 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.example.hibernate.dialect.SQLiteDialect
окт. 01, 2022 8:33:55 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
****Hibernate:
select task0_.id as id1_0_, task0_.status as status2_0_, task0_.task as task3_0_, task0_.task_create_time as task_cre4_0_ from todo task0_
personList.isEmpty() = false**** <----- here result

ClassNotFoundException thrown by Hibernate buildSessionFactory

I am learning hibernate for the first time so unable to find the error.
The student007 table is created in the database then i executed the client class. By analyzing the error what i understand is there is a problem with the sessionfactory part. also there is an error with the oracle driver saying class not found exception
student.hbm.xml
<!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="beans.Student" table="student007" schema="system">
<id name="id" column="sid"/>
<property name="name" column="sname"/>
<property name="email" column="semail"/>
<property name="marks" column="smarks"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<!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="connection.driver_class">oracle.jdbc.oracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">manager</property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<mapping resource="resource/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
client.java
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import beans.Student;
public class Client {
public static void main(String[] args) {
Student st = new Student();
st.setId(111);
st.setName("Ishan");
st.setEmail("abx#gmail.com");
st.setMarks(90);
Configuration cfg = new Configuration();
cfg.configure("resource/hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
//The error appears to be in the above line while building the session factory.
Session s = sf.openSession();
s.save(st);
s.beginTransaction().commit();
s.evict(st);
}
}
student.java
package beans;
public class Student {
private int id;
private String name;
private String email;
private int marks;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public Student() {
// TODO Auto-generated constructor stub
}
}
I am getting this error please help
Jan 16, 2017 7:10:09 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.6.Final}
Jan 16, 2017 7:10:09 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 16, 2017 7:10:10 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jan 16, 2017 7:10:10 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:267)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:231)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:240)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at test.Client.main(Client.java:21)
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [oracle.jdbc.oracleDriver]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:348)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:160)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:116)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:100)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:72)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:240)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:257)
... 14 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : oracle.jdbc.oracleDriver
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:336)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:345)
... 26 more
The exception message says it all: Could not load requested class : oracle.jdbc.oracleDriver
So the first thing I check if you misspelled the name of the driver and you really do. The class names starts with upper-case letters.
The correct name of the driver should be: oracle.jdbc.OracleDriver.

Could not instantiate persister org.hibernate.persister.entity.JoinedSubclassEntityPersister

I am getting the following error while trying to connect to mysql using hibernate. I have a Super class Employee and three subclasses: SoftwareEmployee, HardWareEmployee and Admin.I also have two xml files: hibernateisapersubclass.cfg.xml and employeeisapersubclass.hbm.xml. I am trying to populate data in 4 different table using 'joined-subclass' tag in my xml. Please help.
Error that I am getting are as follows:
Dec 04, 2016 7:45:34 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.5.Final}
Dec 04, 2016 7:45:34 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 04, 2016 7:45:34 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/hqlstudent]
Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Dec 04, 2016 7:45:35 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Sun Dec 04 19:45:35 EST 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Dec 04, 2016 7:45:35 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Dec 04, 2016 7:45:36 PM org.hibernate.AssertionFailure <init>
ERROR: HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: Table hqlstudent.superemployee not found
Dec 04, 2016 7:45:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost/hqlstudent]
Exception in thread "main" org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.JoinedSubclassEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:112)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:128)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:297)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:445)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at test.ClientIsAPerSubClass.main(ClientIsAPerSubClass.java:22)
Caused by: org.hibernate.AssertionFailure: Table hqlstudent.superemployee not found
at org.hibernate.persister.entity.AbstractEntityPersister.getTableId(AbstractEntityPersister.java:5168)
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:433)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
... 7 more
Following are my five java files and two xml files:
ClientIsAPerSubClass.java
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import beans.Admin;
import beans.Admin1;
import beans.HardwareEmployee;
import beans.HardwareEmployee1;
import beans.SoftwareEmployee;
import beans.SoftwareEmployee1;
public class ClientIsAPerSubClass {
public static void main(String [] args){
Configuration configuration = new Configuration();
configuration.configure("resources/hibernateisapersubclass.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction= session.beginTransaction();//student object will move database;
SoftwareEmployee1 softwareEmployee= new SoftwareEmployee1();
softwareEmployee.setId(1);
softwareEmployee.setName("bishwash");
softwareEmployee.setEmail("20wash.sharma#gmail.com");
softwareEmployee.setSalary(60000);
softwareEmployee.setTool("hibernate");
HardwareEmployee1 hardwareEmployee = new HardwareEmployee1();
hardwareEmployee.setId(2);
hardwareEmployee.setName("mahendra");
hardwareEmployee.setEmail("mahendra#mahendra.com");
hardwareEmployee.setSalary(55000);
hardwareEmployee.setWorkinghour(20);
Admin1 admin= new Admin1();
admin.setId(3);
admin.setName("ashish#ashish.com");
admin.setEmail("mahendra#mahendra.com");
admin.setSalary(60000);
admin.setBranchname("Kathmandu");;
session.save(softwareEmployee);
session.save(hardwareEmployee);
session.save(admin);
transaction.commit();
session.close();
sessionFactory.close();
//System.out.println("no of rows dumped from old to new table"+i);
}
}
Admin.java
package beans;
public class Admin extends Employee {
private String branchname;
public Admin() {
}
public Admin(int id, String name, String email, double salary, String branchname) {
super(id, name, email, salary);
this.branchname= branchname;
}
public String getBranchname() {
return branchname;
}
public void setBranchname(String branchname) {
this.branchname = branchname;
}
}
package beans;
public class HardwareEmployee extends Employee {
private int workinghour;
public HardwareEmployee() {
super();
// TODO Auto-generated constructor stub
}
public HardwareEmployee(int id, String name, String email, double salary, int workinghour) {
super(id, name, email, salary);
this.workinghour= workinghour;
}
public int getWorkinghour() {
return workinghour;
}
public void setWorkinghour(int workinghour) {
this.workinghour = workinghour;
}
}
SoftwareEmployee.java
package beans;
public class SoftwareEmployee extends Employee {
private String tool;
public SoftwareEmployee() {
super();
// TODO Auto-generated constructor stub
}
public SoftwareEmployee(int id, String name, String email, double salary ,String tool) {
super(id, name, email, salary);
this.tool= tool;
}
public String getTool() {
return tool;
}
public void setTool(String tool) {
this.tool = tool;
}
}
HardwareEmployee.java
package beans;
public class HardwareEmployee extends Employee {
private int workinghour;
public HardwareEmployee() {
super();
// TODO Auto-generated constructor stub
}
public HardwareEmployee(int id, String name, String email, double salary, int workinghour) {
super(id, name, email, salary);
this.workinghour= workinghour;
}
public int getWorkinghour() {
return workinghour;
}
public void setWorkinghour(int workinghour) {
this.workinghour = workinghour;
}
}
employeeisapersubclass.hbm.xml
<!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="beans.Employee" table ="superemployee" schema="hqlstudent">
<id name="id"/>
<property name="name" />
<property name="email" />
<property name="salary" />
<joined-subclass name="beans.SoftwareEmployee" table="softwareemployee">
<key column ="id"/>
<property name="tool"/>
</joined-subclass>
<joined-subclass name="beans.HardwareEmployee" table="hardwareemployee">
<key column ="id"/>
<property name="workinghour"/>
</joined-subclass>
<joined-subclass name="beans.Admin" table="admin">
<key column ="id"/>
<property name="branchname"/>
</joined-subclass>
</class>
</hibernate-mapping>
hibernateisapersubclass.cfg.xml
<!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.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.hbm2ddl.auto">
create
</property>
<property name="hibernate.show_sql">
true
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume customstudent is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hqlstudent
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
password
</property>
<mapping resource="resources/employeeisapersubclass.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Mapping (RESOURCE) not found

Guys I am new with Hibernate and I am struggling to make a hello world app work. Below I will post the exception I am getting as well as my code - with the hope maybe someone can spot other errors which I have and save me from "future" troubles.
Code:
package org.arpit.javapostsforlearning;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
#Entity(name = "User_table")
public class User {
#Id
int userId;
#Column(name = "User_Name")
String userName;
String userMessage;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserMessage() {
return userMessage;
}
public void setUserMessage(String userMessage) {
this.userMessage = userMessage;
}
}
This:
package org.arpit.javapostsforlearning;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateMain {
public static void main(String[] args)
{
Configuration configuration=new Configuration();
configuration.configure();
configuration.addClass(org.arpit.javapostsforlearning.User.class);
ServiceRegistry sr= new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sf=configuration.buildSessionFactory(sr);
User user1=new User();
user1.setUserName("Arpit");
user1.setUserMessage("Hello world from arpit");
User user2=new User();
user2.setUserName("Ankita");
user2.setUserMessage("Hello world from ankita");
Session ss=sf.openSession();
ss.beginTransaction(); //saving objects to session
ss.save(user1);
ss.save(user2);
ss.getTransaction().commit();
ss.close();
}
}
And XMLS:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;database=Sandboxlearning;integratedSecurity=true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServer2005Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.hbm.xml
<?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="User" table="User_table">
<meta attribute="class-description">
This class contains the user detail.
</meta>
<id name="userId" type="int" column="userId">
<generator class="native"/>
</id>
<property name="userName" column="User_Name" type="string"/>
<property name="userMessage" column="userMessage" type="string"/>
</class>
</hibernate-mapping>
Exception:
Jun 28, 2016 7:17:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 28, 2016 7:17:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 28, 2016 7:17:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 28, 2016 7:17:15 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Exception in thread "main" org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : org/arpit/javapostsforlearning/User.hbm.xml : origin(org/arpit/javapostsforlearning/User.hbm.xml)
at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56)
at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274)
at org.hibernate.boot.MetadataSources.addClass(MetadataSources.java:262)
at org.hibernate.cfg.Configuration.addClass(Configuration.java:513)
at org.arpit.javapostsforlearning.HibernateMain.main(HibernateMain.java:14)
The XMLS are stored under src folder.
Here is also screen shot of my App
image
Take a look to: <mapping class="User.hbm.xml"/>. But you using resource. You are able to use class when you saying that map me class for that, but you want to map resource. So just use
<mapping resource="User.hbm.xml"/>

Categories

Resources