保存Hibernate InvocationTargetException

时间:2015-12-01 15:07:14

标签: java mysql hibernate

我正在尝试将Hibernate 5添加为ORM,以便后端连接MySQL数据库。我阅读了很多示例和教程,但我总是得到一个InvocationTargetException。

以下是相关代码。希望有人可以帮助我。

的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     包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();
    }
}

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)

非常感谢!

1 个答案:

答案 0 :(得分:0)

好的,我自己找到了答案。问题是我初始化配置的方式,如果我理解正确的话。

我把它改为:

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

现在它有效! : - )