Facing hibernate error: Caused by: java.lang.ClassNotFoundException: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver
Asked Answered
B

9

9

I am getting an error while running this code in Eclipse. I have created Student.java file:

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

I have created Student.hbm.xml file:

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

I have created hibernate.cfg.xml file:

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

I have created client.java file:

    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("[email protected]");
        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);
    }
}

I tried adding many jars file but I am unable see student record in my oracle database.

Full error:

    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
Betz answered 17/7, 2018 at 14:56 Comment(0)
T
12

I had the same problem with this library.

In the Java Build Path (Project Properties ) did you add the jar files to the Modulepath or to the Classpath ? I initially added the jars under the Modulepath. While debugging the issue I decided to move the jars into the Classpath . This resolved the problem.

Another option is to convert the project to a Maven project

  1. Right click the project name in the package explorer

  2. Scroll down to Configure

  3. Select Convert to Maven project

  4. Accept all the defaults and click on Finish

Thirtieth answered 18/5, 2020 at 1:2 Comment(1)
Where can I find an explanation? I do not know whly Modulepath or Classpath are so important in Eclipse.Schappe
D
8

I have the same problem: turns out the net-buddy library file was corrupted (error during download?).

Try deleting jars in ~/.m2/repository/net/bytebuddy and rebuild the application.

Hope this helps.

Duplicature answered 1/10, 2018 at 19:17 Comment(1)
this is only for MAVEN project scenery.Aventurine
F
4

I had the same error with this library when using it with maven. The dependency was transitively coming from hibernet-core. When checked with maven dependency:tree, the dependency was on test score for some reason. I had to include the dependency in my pom with provided scope and that fixed the issue.

    <dependency>
        <groupId>net.bytebuddy</groupId>
        <artifactId>byte-buddy</artifactId>
        <version>1.6.14</version>
        <scope>provided</scope>
    </dependency>
Flashgun answered 6/12, 2022 at 23:17 Comment(0)
S
3

Place the hibernate jar files in the classpath. This will most likely resolve the issue.

Sizzler answered 8/1, 2020 at 23:50 Comment(0)
M
3

In Java Modular System this still pops up - You do need to in your module-info specify your module uses bytebuddy

requires net.bytebuddy;

which will force you to place it in the classpath :)

Milkmaid answered 30/3, 2020 at 21:42 Comment(0)
T
2

The bytebuddy dependency is used by Hibernate. I had a similar problem and I've solved my problem by adding the following dependency in my pom.xml.

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

Go to this link for the latest version of maven dependency. This solution should work for Spring Boot based projects.

Tailgate answered 14/4, 2020 at 13:7 Comment(0)
P
2

Try to add byte-buddy-X.XX.XX.jar to the Classpath of the project. If U work in Eclipse, just: right click on the project name, then select Properties -> Java Build Path -> Libraries and add to the Classpath this this file.

For me was helpful to delete byte-buddy-X.XX.XX.jar from Modulepath and then added it to the Classpath.

Petta answered 29/4, 2020 at 6:23 Comment(0)
M
0

I tried couple of solutions for this but it is quite simple. Simply move all Jar files from ModulePath to ClassPath and then Clean and Build the project. Your project will start working as expected.

Minimal answered 7/4, 2022 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.