【问题标题】:Facing hibernate error: Caused by: java.lang.ClassNotFoundException: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver面临休眠错误:引起:java.lang.ClassNotFoundException:net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver
【发布时间】:2018-12-25 08:13:18
【问题描述】:

在 Eclipse 中运行此代码时出现错误。我创建了Student.java 文件:

public class Student {
    private int id;
    private String name;
    private String email;
    private int marks;
    
    // getters/setters
}

我已经创建了Student.hbm.xml 文件:

<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
          
    <hibernate-mapping>
    <class name="bean.Student" table="student">
            <id name="id" column="sid"></id>
            <property name="name"  column="sname"></property>
            <property name="email" column="semail"></property>
            <property name="marks" column="smarks"></property>
    </class>
    </hibernate-mapping>

我已经创建了hibernate.cfg.xml 文件:

 <!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">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:wind</property>
        <property name="connection.username">localuser</property>
        <property name="connection.password">localuser</property>
        <property name="connection.poolsize">5</property>
            
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>
            
        <mapping resource="resources/student.hbm.xml"/>
            
    </session-factory>
    </hibernate-configuration>

我已经创建了client.java 文件:

    package testclass;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import bean.Student;
    
    public class Client {
    public static void main(String[] args) {
        
        Student st = new Student();
        st.setId(11);
        st.setEmail("swapnilgthaware@gmail.com");
        st.setMarks(98);
        st.setName("Swapnil");  
        
        // Student object is transient here..
        // When it is attached to hibernate object then it will become persistent object.
        
        
        Configuration cfg = new Configuration();
        cfg.configure("resources/hibernate.cfg.xml");
        
        SessionFactory sf = cfg.buildSessionFactory();
        Session s =sf.openSession();
        
        s.save(st);
        
        // Student object is persisten now. Even gc() will not take away this object
        
        s.beginTransaction().commit();
        // Student object will goto Database side.
        
        s.evict(st);
    }
}

我尝试添加许多 jars 文件,但在我的 oracle 数据库中看不到学生记录。

完全错误:

    Jul 17, 2018 8:11:09 PM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {5.3.2.Final}
    Jul 17, 2018 8:11:09 PM org.hibernate.cfg.Environment <clinit>
    INFO: HHH000206: hibernate.properties not found
    Exception in thread "main" java.lang.NoClassDefFoundError: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver
        at org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:357)
        at org.hibernate.cfg.Environment.buildBytecodeProvider(Environment.java:352)
        at org.hibernate.cfg.Environment.<clinit>(Environment.java:246)
        at org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:78)
        at org.hibernate.boot.registry.StandardServiceRegistryBuilder.<init>(StandardServiceRegistryBuilder.java:67)
        at org.hibernate.cfg.Configuration.reset(Configuration.java:158)
        at org.hibernate.cfg.Configuration.<init>(Configuration.java:124)
        at org.hibernate.cfg.Configuration.<init>(Configuration.java:118)
        at testclass.Client.main(Client.java:21)
    Caused by: java.lang.ClassNotFoundException: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 9 more

【问题讨论】:

    标签: java spring hibernate classnotfoundexception


    【解决方案1】:

    我对这个库也有同样的问题。

    在 Java 构建路径(项目属性)中,您是否将 jar 文件添加到 Modulepath 或 Classpath ?我最初在 Modulepath 下添加了罐子。在调试问题时,我决定将 jar 移到 Classpath 中。这解决了问题。

    另一种选择是将项目转换为 Maven 项目

    1. 在包资源管理器中右键项目名称

    2. 向下滚动到配置

    3. 选择转换为 Maven 项目

    4. 接受所有默认设置并点击完成

    【讨论】:

    • 在哪里可以找到解释?我不知道为什么 Modulepath 或 Classpath 在 Eclipse 中如此重要。
    【解决方案2】:

    我有同样的问题:原来 net-buddy 库文件已损坏(下载时出错?)。

    尝试删除 ~/.m2/repository/net/bytebuddy 中的 jars 并重建应用程序。

    希望这会有所帮助。

    【讨论】:

    • 这仅适用于 MAVEN 项目风景。
    【解决方案3】:

    将休眠 jar 文件放在类路径中。这很可能会解决问题。

    【讨论】:

      【解决方案4】:

      在 Java 模块化系统中,这仍然会弹出 - 您确实需要在您的模块信息中指定您的模块使用 bytebuddy

      requires net.bytebuddy;
      

      这将迫使您将其放置在类路径中:)

      【讨论】:

        【解决方案5】:

        尝试将 byte-buddy-X.XX.XX.jar 添加到项目的 Classpath 中。如果 U 在 Eclipse 中工作,只需:右键单击项目名称,然后选择 Properties -> Java Build Path -> Libraries 并将此文件添加到 Classpath 中。

        对我来说,从 Modulepath 中删除 byte-buddy-X.XX.XX.jar 然后将其添加到 Classpath 中很有帮助。

        【讨论】:

          【解决方案6】:

          bytebuddy 依赖被 Hibernate 使用。我遇到了类似的问题,我通过在 pom.xml 中添加以下依赖项解决了我的问题。

          <dependency>
              <groupId>net.bytebuddy</groupId>
              <artifactId>byte-buddy-dep</artifactId>
              <version>1.10.9</version>
          </dependency>
          

          转到link 获取最新版本的 maven 依赖项。此解决方案应该适用于基于 Spring Boot 的项目。

          【讨论】:

            【解决方案7】:
            猜你喜欢
            • 2020-04-11
            • 1970-01-01
            • 1970-01-01
            • 2012-05-14
            • 2019-01-03
            • 1970-01-01
            • 1970-01-01
            • 2012-04-19
            相关资源
            最近更新 更多