Hibernate无法保存对象

时间:2015-08-23 12:26:23

标签: java mysql hibernate

我正在从PluralSight做关于Hibernate的入门教程。 对于我的数据库,我使用的是MYSQL。

在我的main方法中,我正在创建一个对象,我正在尝试保存它。 但是,我收到以下错误:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.simpleprogrammer.User
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1447)
    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 com.simpleprogrammer.Program.main(Program.java:16)

我的主要方法是:

public static void main(String[] args) {
        System.out.println("Hello World!");
        Session session = HibernateUtilities.getSessionFactory().openSession();
        session.beginTransaction();

        User user = new User();
        user.setName("Joe");
        user.setGoal(250);
        session.save(user);

        session.getTransaction().commit();
        session.close();
        HibernateUtilities.getSessionFactory().close();
    }

错误显示在session.save(user);

的行上

我的实用工具课程:

package com.simpleprogrammer;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


public class HibernateUtilities {

    private static SessionFactory sessionFactory;
    private static StandardServiceRegistryBuilder ssrb;

    static
    {
        try
        {
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

            ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(ssrb.build());
        }
        catch(HibernateException exception)
        {
            System.out.println("Problem creating session factory");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }


}

我的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">appuser</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3307</property>
  <property name="hibernate.connection.username">appuser</property>
  <property name="hibernate.default_schema">protein_tracker</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <mapping resource="com/simpleprogrammer/User.hbm.xml"/>
 </session-factory
</hibernate-configuration>

很抱歉发布所有这些代码,但我不确定哪个部分是相关的。 我怀疑我的错误是由数据库连接引起的,但我不知道如何检查它。

2 个答案:

答案 0 :(得分:0)

我在线上有句法错误:

</session-factory

这是hibernate.cfg.xml

答案 1 :(得分:0)

这是我对stackoverflow.com的第一次贡献。我面临类似的错误,但现在我通过这个给定的解决方案解决了它。所以,我正在写这个答案。

您必须像这样更改实用程序文件:

static
{
    try
    {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");

        //ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        sessionFactory = configuration.buildSessionFactory();
    }
    catch(HibernateException exception)
    {
        System.out.println("Problem creating session factory");
    }
}


public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
  1. 必须删除ssrb变量声明和定义。
  2. configuration.buildSessionFactory(); 代替 configuration.buildSessionFactory(ssrb.build());