Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NoClassDefFoundError-ClassLoaderDelegate-Hibernate_Java_Eclipse_Hibernate - Fatal编程技术网

Java NoClassDefFoundError-ClassLoaderDelegate-Hibernate

Java NoClassDefFoundError-ClassLoaderDelegate-Hibernate,java,eclipse,hibernate,Java,Eclipse,Hibernate,我不熟悉Java Hibernate。我在eclipse中设置了一个动态web项目,并尝试在mysql数据库中插入一行。在服务器上运行代码时,出现以下错误 java.lang.NoClassDefFoundError:org/hibernate/annotations/common/reflection/ClassLoaderDelegate org.hibernate.boot.internal.MetadataBuilderImpl.(MetadataBuilderImpl.java:127

我不熟悉Java Hibernate。我在eclipse中设置了一个动态web项目,并尝试在mysql数据库中插入一行。在服务器上运行代码时,出现以下错误

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)

我的hibernate.cfg.xml

我的主要方法是

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;
    }

}
这是参考的图书馆。


请帮我解决这个问题。我在谷歌上搜索,找不到解决方案。

您引用的库似乎不匹配。例如,我看到了
hibernate commons annotations
的4.0.4.Final版本和
hibernate core
的5.0.0.Final版本。annotations包的5.0.0版随Hibernate 5.0.0一起提供。我还看到了
hibernate-jpa-2.0
(版本1.0.0.Final),而hibernate 5.0.0附带的是
hibernate-jpa-2.1
。可能还有其他不匹配之处。这些不匹配似乎可以解释你的问题。@JohnBollinger这就是问题所在。我添加了正确版本的JAR,现在效果很好。非常感谢你。
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();
        }

    }
    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;
    }

}