0

My question might look a easy and silly but I'm unable to find solution so thought of sharing the problem here... I'm getting an Exception in a simple Hibernate code running in Java perspective!! I've added JTA or transaction jar files but it's not helping!

package com.demo.bean;
import java.util.Date;

import javax.persistence.Column; 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="emp_details")

public class Employee{
    @Id
    @Column(name="emp_id")
    private int id;
    @Column(name="emp_name")
    private String name;
    private String address;
    private int phone;
    private double salary;
    @Lob
    private String description;
    @Temporal(TemporalType.DATE)
    private Date doj;

    public Date getDoj() {
        return doj;
    }
    public void setDoj(Date doj) {
        this.doj = doj;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    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 getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getPhone() {
        return phone;
    }
    public void setPhone(int phone) {
        this.phone = phone;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
 }

package com.demo.client;

import java.util.Date;

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

import com.demo.bean.Employee;

public class HibernateClient {

    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.setId(333);
        emp.setName("Guru");
        emp.setAddress("Bangalore");
        emp.setSalary(56565.0);
        emp.setDescription("Hello");
        emp.setDoj(new Date());

        Configuration cfg = new Configuration();
        cfg.configure("resource/hibernate.cfg.xml");
        SessionFactory sessionFactory = cfg.buildSessionFactory();

        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(emp);
        tx.commit();

        sessionFactory.close();
        }
   }

<?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>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/becm3createDatabaseIfNotExist=true</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.dialect">org.hibernate.dialect.MySQL5Dialect</property>

<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<mapping class="com.demo.bean.Employee"/>

</session-factory>
</hibernate-configuration>
MWiesner
  • 8,868
  • 11
  • 36
  • 70
  • Possible duplicate of [Hibernate 4 javax/transaction/SystemException error](http://stackoverflow.com/questions/18125294/hibernate-4-javax-transaction-systemexception-error) – Sully Jan 18 '16 at 16:20
  • I'm using Hibernate version 5...I tried jta 1.1 jar too but still no solution – Adithyavarma Jan 18 '16 at 17:06
  • Make sure it is in the classpath – Sully Jan 18 '16 at 17:30
  • Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.jdbc.Driver] – Adithyavarma Jan 18 '16 at 17:41
  • Same issue different JAR. Add all dependencies. – Sully Jan 18 '16 at 18:23

0 Answers0