Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Hibernate Basics Persisting Data with Hibernate Saving Data with Hibernate

shekhar bhardwaj
seal-mask
.a{fill-rule:evenodd;}techdegree
shekhar bhardwaj
Full Stack JavaScript Techdegree Student 12,373 Points

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver while H2 Connection.

Following Exception is thrown :

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/classmate/TypeResolver
    at org.hibernate.metamodel.source.annotations.AnnotationBindingContextImpl.<init>(AnnotationBindingContextImpl.java:53)
    at org.hibernate.metamodel.source.annotations.AnnotationMetadataSourceProcessorImpl.prepare(AnnotationMetadataSourceProcessorImpl.java:105)
    at org.hibernate.metamodel.source.internal.MetadataImpl.prepare(MetadataImpl.java:175)
    at org.hibernate.metamodel.source.internal.MetadataImpl.<init>(MetadataImpl.java:160)
    at org.hibernate.metamodel.source.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:123)
    at org.hibernate.metamodel.MetadataSources.buildMetadata(MetadataSources.java:151)
    at com.hibernate.demo.Application.buildSesssionFactory(Application.java:54)
    at com.hibernate.demo.Application.<clinit>(Application.java:21)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.classmate.TypeResolver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 8 more

When I am trying to

return new MetadataSources(registry).buildMetadata().buildSessionFactory();

here is my Application class:

package com.hibernate.demo;



import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;

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

import com.hibernate.demo.model.Contact;

public class Application {

    //Session factory
    private static final SessionFactory sessionFactory = buildSesssionFactory();

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Session factory");
        Contact contact = new Contact.ContactBuilder("Bob", "Marley").withEmail("bob.marley@gmail.com").withPhone(555555555L).build();

        //Open a Session
        System.out.println("Open a Session");
        Session session = sessionFactory.openSession();

        //Begin a Transaction
        System.out.println("Begin a Transaction");
         session.beginTransaction();


        //Use the session to save the contact
        System.out.println("Use the session to save the contact");
        session.save(contact);
        //Commit the transaction
        System.out.println("Commit the transaction");
        session.getTransaction().commit();
        // Close the session
        System.out.println("Close the session");
        session.close();
    }

    private static SessionFactory buildSesssionFactory() {
        // Create a StandardServiceRegistry
        Configuration configuration = new Configuration();
        configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults","false");
        configuration.configure("hibernate.cfg.xml");
        final ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        return new MetadataSources(registry).buildMetadata().buildSessionFactory();


    }

    static boolean isLocked(String fileName) {
        try {
            RandomAccessFile f = new RandomAccessFile(fileName, "r");
            try {
                FileLock lock = f.getChannel().tryLock(0, Long.MAX_VALUE, true);
                if (lock != null) {
                    lock.release();
                    return false;
                }
            } finally {
                f.close();
            }
        } catch (IOException e) {
            // ignore
        }
        return true;
    }

}

1 Answer

shekhar bhardwaj
seal-mask
.a{fill-rule:evenodd;}techdegree
shekhar bhardwaj
Full Stack JavaScript Techdegree Student 12,373 Points

issue is resolved, but I am not sure if I am on the right track .

my buildSessionFactory function now look like this-

private static SessionFactory buildSesssionFactory() {
        // Create a StandardServiceRegistry
        Configuration configuration = new Configuration();
        configuration.setProperty("hibernate.temp.use_jdbc_metadata_defaults","false");
        configuration.configure("hibernate.cfg.xml");
        final ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        return new org.hibernate.boot.MetadataSources(registry).buildMetadata().buildSessionFactory();

Which is weird, apparently I started with Spring-boot framework to learn hibernate with Spring and diverted to hibernate basic and I used the same spring-boot project to learn hibernate basics. now I am stuck at

*Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernate.demo.model.Contact
    at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1596)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:668)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:660)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:655)
    at com.hibernate.demo.Application.main(Application.java:38)*