Search code examples
javamysqlhibernate

Connection issue in hibernate


I need to connect my song class with the database using hibernate. But, hibernate is not connecting and excutting the queries. I used several methods and several dependencies of hibernate but still it is not executing.

Song.java

package com.projects.demohibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity(name="Song")
public class Song {

    @Id
    @Column(name = "id")
    private int id;
    @Column(name = "Songname")
    private String Songname;

    @Column(name = "Artist")
    private String Artist;

    public Song(int id, String songname, String artist) {
        this.id = id;
        Songname = songname;
        Artist = artist;
    }

    public void setId(int id) {
        this.id = id;
    }
    public void setSongname(String songname) {
        Songname = songname;
    }
    public void setArtist(String artist) {
        Artist = artist;
    }
}

And my main file Source.java

package com.projects.demohibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Source {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();

        configuration.configure("hibernate.cfg.xml");

        configuration.addAnnotatedClass(Song.class);
        SessionFactory sessionFactory = configuration.buildSessionFactory();

        try (Session session = sessionFactory.openSession()) {
            Song song1 = new Song();
            song1.setId(1);
            song1.setSongname("Cameo");
            song1.setArtist("James Cameo");
            System.out.println("Song saved");
            session.beginTransaction();
            session.save(song1);
            session.getTransaction().commit();
            sessionFactory.close();

        }
    }
}

My 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="hibernate.connection.url">jdbc:mysql://localhost:3306/Song</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <!-- DB schema will be updated if needed -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.show_sql">true</property>
name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>


  </session-factory>
</hibernate-configuration>

I need to connect my Song class with my MySql database with the help of hibernate.


Solution

  • Try to switch the javax package to jakarta package in Song file. It worked for me and i think it will work for you as well.

    Add the dependency in the pom.xml file

    <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>