1

I am trying to add Hibernate 5 as ORM for a backend to connect with a MySQL-database. I read many examples and tutorials but I always get an InvocationTargetException.

Here are the relevant code. Hope someone can help me.

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cooking</property>

            <property name="connection.username">root</property>
            <property name="connection.password"></property>
            <property name="hibernate.current_session_context_class">thread</property>
            <property name="hibernate.hbm2ddl.auto">update</property>

            <mapping class="de.fani.cooking.object.User"/>
       </session-factory>
    </hibernate-configuration>

HibernateUtil.java

package de.fani.cooking.hibernate;

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 SessionFactory sessionFactory;

    private static SessionFactory buildSessionFactory()
    {
        try
        {
            // Create session factory from cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");

            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                                                    .applySettings(configuration.getProperties())
                                                    .build();

            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

            return sessionFactory;
         }
        catch (Throwable ex)
        {
            System.err.println("Initial session factory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory()
    {
        if (sessionFactory == null)
        {
            sessionFactory = buildSessionFactory();
        }
        return sessionFactory;
    }

}

User.java package de.fani.cooking.object;

import javax.persistence.*;

@Entity (name = "User")
@Table(name="User", uniqueConstraints={@UniqueConstraint(columnNames = {"id"})})
public class User
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column (name = "id", nullable = false, unique = true, length = 11)
    private int id;

    @Column (name = "username", nullable = false, length = 30)
    private String username;

    @Column (name = "password", nullable = false, length = 256)
    private String password;


    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

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

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User()
    {

    }
}

SQL

CREATE TABLE `User` (
 `id` int(11) NOT NULL,
 `username` varchar(30) NOT NULL,
 `password` varchar(256) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

UserResource.java

package de.fani.cooking.resource;

import de.fani.cooking.hibernate.HibernateUtil;
import de.fani.cooking.object.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/user")
public class UserResource
{
    @POST
    @Path("register")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response register(User _user)
    {
        User test = new User();
        test.setId(24);
        test.setUsername("tester");
        test.setPassword("secret");

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        sessionFactory.openSession();
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        session.save(test);
        session.getTransaction().commit();
        session.flush();

        return Response.status(Response.Status.OK).entity(_user).build();
    }
}

Exception at session.save(test);:

SCHWERWIEGEND: Servlet.service() for servlet [ApplicationConfig] in context with path [] threw exception [org.hibernate.MappingException: Unknown entity: de.fani.cooking.object.User] with root cause
org.hibernate.MappingException: Unknown entity: de.fani.cooking.object.User
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1462)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
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:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at com.sun.proxy.$Proxy58.save(Unknown Source)

Thanks a lot!

1 Answer 1

1

Ok, I found the answer by myself. The problem was the way I initialized the configuration, if I understand it correctly.

I changed it to:

private static SessionFactory buildSessionFactory()
{
    try
    {
        // Create session factory from cfg.xml
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                                                .configure("hibernate.cfg.xml")
                                                .build();


        Metadata Meta = new MetadataSources(serviceRegistry)
                        .addAnnotatedClass(User.class)
                        .addAnnotatedClassName("de.fani.cooking.model.User")
                        .getMetadataBuilder()
                        .applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
                        .build();

        SessionFactory sessionFactory = Meta.getSessionFactoryBuilder()
                                        .build();

        return sessionFactory;
    }
    catch (Throwable ex)
    {
        System.err.println("Initial session factory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

And now it works! :-)

Sign up to request clarification or add additional context in comments.

2 Comments

Do you have to add every annotated POJO class to the Meta?
@Duy Yes, I did it, but I think it is not needed depending on your XML. Maybe there is also a better solution.

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.