Could someone please help me figure out why I’m getting the following problem?:
/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -javaagent:/home/user/idea-IC-232.9559.62/lib/idea_rt.jar=45059:/home/user/idea-IC-232.9559.62/bin -Dfile.encoding=UTF-8 -classpath /home/user/IdeaProjects/HibernateLearningVideos03And04/target/classes:/home/user/.m2/repository/org/hibernate/orm/hibernate-core/6.2.7.Final/hibernate-core-6.2.7.Final.jar:/home/user/.m2/repository/jakarta/persistence/jakarta.persistence-api/3.1.0/jakarta.persistence-api-3.1.0.jar:/home/user/.m2/repository/jakarta/transaction/jakarta.transaction-api/2.0.1/jakarta.transaction-api-2.0.1.jar:/home/user/.m2/repository/org/jboss/logging/jboss-logging/3.5.0.Final/jboss-logging-3.5.0.Final.jar:/home/user/.m2/repository/org/hibernate/common/hibernate-commons-annotations/6.0.6.Final/hibernate-commons-annotations-6.0.6.Final.jar:/home/user/.m2/repository/io/smallrye/jandex/3.0.5/jandex-3.0.5.jar:/home/user/.m2/repository/com/fasterxml/classmate/1.5.1/classmate-1.5.1.jar:/home/user/.m2/repository/net/bytebuddy/byte-buddy/1.12.18/byte-buddy-1.12.18.jar:/home/user/.m2/repository/jakarta/xml/bind/jakarta.xml.bind-api/4.0.0/jakarta.xml.bind-api-4.0.0.jar:/home/user/.m2/repository/jakarta/activation/jakarta.activation-api/2.1.0/jakarta.activation-api-2.1.0.jar:/home/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/4.0.2/jaxb-runtime-4.0.2.jar:/home/user/.m2/repository/org/glassfish/jaxb/jaxb-core/4.0.2/jaxb-core-4.0.2.jar:/home/user/.m2/repository/org/eclipse/angus/angus-activation/2.0.0/angus-activation-2.0.0.jar:/home/user/.m2/repository/org/glassfish/jaxb/txw2/4.0.2/txw2-4.0.2.jar:/home/user/.m2/repository/com/sun/istack/istack-commons-runtime/4.1.1/istack-commons-runtime-4.1.1.jar:/home/user/.m2/repository/jakarta/inject/jakarta.inject-api/2.0.1/jakarta.inject-api-2.0.1.jar:/home/user/.m2/repository/org/antlr/antlr4-runtime/4.10.1/antlr4-runtime-4.10.1.jar:/home/user/.m2/repository/org/xerial/sqlite-jdbc/3.42.0.0/sqlite-jdbc-3.42.0.0.jar:/home/user/.m2/repository/org/hibernate/orm/hibernate-community-dialects/6.2.7.Final/hibernate-community-dialects-6.2.7.Final.jar com.mycompany.hibernatelearningvideos03and04.client.ClientCode
Hello World!
Sep. 06, 2023 6:09:52 A.M. org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.2.7.Final
Sep. 06, 2023 6:09:52 A.M. org.hibernate.cfg.Environment <clinit>
INFO: HHH000406: Using bytecode reflection optimizer
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:244)
at org.hibernate.cfg.Configuration.configure(Configuration.java:358)
at org.hibernate.cfg.Configuration.configure(Configuration.java:343)
at com.mycompany.hibernatelearningvideos03and04.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
at com.mycompany.hibernatelearningvideos03and04.client.ClientCode.main(ClientCode.java:18)
Process finished with exit code 0
ClientCode.java:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.hibernatelearningvideos03and04.client;
import com.mycompany.hibernatelearningvideos03and04.model.Student;
import com.mycompany.hibernatelearningvideos03and04.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class ClientCode {
public static void main(String[] args) {
System.out.println("Hello World!");
Transaction tx = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
Student st = new Student();
st.setId(1);
st.setName("Hibernate");
st.setEmail("hibernate@123");
st.setPhone(12345);
session.save(st);
} catch(Exception e) {
//
}
}
}
Student.java:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.hibernatelearningvideos03and04.model;
public class Student {
private int id;
private String name;
private String email;
private long phone;
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 long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
}
student.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 = "Student" table = "students">
<id name = "id" type = "int" column = "id">
<generator class="increment"/>
</id>
<property name = "name" column = "name" type = "string"/>
<property name = "email" column = "email"/> <!-- No need to explicitly state string type. -->
<property name = "phone" column = "phone" type = "Long"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml (found in both /home/user/IdeaProjects/HibernateLearningVideos03And04/src/main/resources/hibernate.cfg.xml and /home/user/IdeaProjects/HibernateLearningVideos03And04/src/hibernate.cfg.xml because I read conflicting information online, so I figured I’d have the same file in both locations):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:mydatabaseyo.sqlite</property>
<!-- SQL dialect for SQLite -->
<!-- <property name="dialect">org.hibernate.dialect.SQLiteDialect</property> -->
<property name="hibernate.dialect">org.hibernate.community.dialect.SQLiteDialect</property> <!-- <property name="hibernate.dialect">org.hibernate.dialect.SQLiteDialect</property> -->
<mapping resource="com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil.java:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.hibernatelearningvideos03and04.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
if(sessionFactory == null) {
try {
Configuration cfg = new Configuration().configure();
///home/user/NetBeansProjects/HibernateLearningVideos03And04/src/main/java/
cfg.addResource("com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml");
StandardServiceRegistry ssRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory(ssRegistry);
} catch(Exception e) {
e.printStackTrace();
}
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>HibernateLearningVideos03And04</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<exec.mainClass>com.mycompany.hibernatelearningvideos03and04.HibernateLearningVideos03And04</exec.mainClass>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>
<!--https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc-->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.42.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-community-dialects -->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-community-dialects</artifactId>
<version>6.2.7.Final</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
Any input would be greatly appreciated!
P.S.
I tried this in NetBeans and then I did this again in IntelliJ Community Edition; the above is via IntelliJ Community Edition.