0

I am new to Java Hibernate. I setup a dynamic web project in eclipse and trying to insert a row in mysql db. I am getting the following error when I run my code on server.

java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ClassLoaderDelegate org.hibernate.boot.internal.MetadataBuilderImpl.(MetadataBuilderImpl.java:127) org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135) org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:655)

My hibernate.cfg.xml

<session-factory>
    <!-- Database connection settings -->
    <property name='connection.driver_class'>com.mysql.jdbc.Driver</property>
    <property name='connection.url'>jdbc:mysql://localhost:3306/bornscientific</property>
    <property name='connection.username'>root</property>
    <property name='connection.password'></property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name='connection.pool_size'>1</property>

    <!-- SQL dialect -->
    <property name='dialect'>org.hibernate.dialect.MySQLDialect</property>

    <!-- Echo all executed SQL to stdout -->
    <property name='show_sql'>true</property>

    <!-- Mapping files -->
    <mapping class="com.bornscientific.persistence.beans.Tags"/>

</session-factory>

My entity class

package com.bornscientific.persistence.beans;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "TAGS")
public class Tags implements Serializable
{

    private Long id;
    private String name;

    public Tags()
    {

    }

    @Id
    @SequenceGenerator(name="seq1",sequenceName="HIB_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq1")
    @Column(name = "TAG_ID", unique = true, nullable = false)
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    @Column(name = "NAME", unique = true, length = 100, nullable = false)
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}

And my main method is

public static void test() throws Exception
    {
        try
        {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx = session.getTransaction();
            tx.begin();

            Tags tags = new Tags();
            tags.setName("TAG_1");

            session.save(tags);
            tx.commit();
            System.out.println("Saved successfully...");
        }
        catch(Exception e)
        {
            //System.out.println(e)
            e.printStackTrace();
        }

    }

HibernateUtil.java

    package com.bornscientific.persistence;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil
{
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory()
    {
        try
        {

            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration.getProperties());
            ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
            return configuration.buildSessionFactory(serviceRegistry);
        }
        catch (Throwable ex)
        {
            System.err.println("Initial SessionFactory creation failed." + ex);
            ex.printStackTrace();
            throw ex;
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

}

And this is the library referenced. enter image description here

Please help me fix this issue. I have searched through google and could not find a solution.

2
  • 2
    Your referenced libraries appear mismatched. For example, I see version 4.0.4.Final of hibernate-commons-annotations and version 5.0.0.Final of hibernate-core. Version 5.0.0 of the annotations package ships with Hibernate 5.0.0. I also see hibernate-jpa-2.0 (version 1.0.0.Final), whereas it is hibernate-jpa-2.1 that ships with Hibernate 5.0.0. There may be other mismatches. It seems likely that these mismatches explain your problem. Commented Oct 7, 2015 at 19:57
  • 1
    @JohnBollinger That was the problem. I added correct versions of jars and it works good now. Thank you so much. Commented Oct 8, 2015 at 18:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.