获取org.hibernate.MappingException:未知实体: [英] Getting org.hibernate.MappingException: Unknown entity:

查看:86
本文介绍了获取org.hibernate.MappingException:未知实体:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我在各地都看到过这个问题,但我仍然无法弄清楚我的代码有什么问题。当然,我正在俯视一些东西,不能只告诉一下。

这是我的POJO。

 包hibernate; 

public class User {
private int id;
私人字符串名称;
private int total;
private int goal;

public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getTotal(){
return total;
}
public void setTotal(int total){
this.total = total;
}
public int getGoal(){
return goal;
}
public void setGoal(int goal){
this.goal = goal;


以下是我的hibernate.cfg.xml

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate配置DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd >
< hibernate-configuration>
< session-factory name =>
< property name =hibernate.connection.driver_class> com.microsoft.sqlserver.jdbc.SQLServerDriver< / property>
< property name =hibernate.connection.url> jdbc:sqlserver:// localhost:1433; DatabaseName = test;< / property>
< property name =hibernate.dialect> org.hibernate.dialect.SQLServerDialect< / property>
< property name =hibernate.connection.username> sa< / property>
< property name =hibernate.connection.password> admin< / property>

< mapping resource =hibernate / User.hbm.xml/>
< / session-factory>
< / hibernate-configuration>

这是我的User.hbm.xml

 <?xml version =1.0?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate映射DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\"> ;
<! - 生成2016年1月18日上午12:10:00由Hibernate Tools 3.5.0.Final - >
< hibernate-mapping>
< class name =hibernate.Usertable =USER>
< id name =idtype =int>
< column name =ID/>
< generator class =native/>
< / id>
< property name =nametype =java.lang.String>
< column name =NAME/>
< / property>
< property name =totaltype =int>
< column name =TOTAL/>
< / property>
< property name =goaltype =int>
< column name =GOAL/>
< / property>
< / class>
< / hibernate-mapping>

以下是主要应用程序将上述内容放在一起:

  package hibernate; 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
导入org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class App {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String [] args){
Configuration configuration = new Configuration()。configure();
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())。build();

sessionFactory = configuration.buildSessionFactory(serviceRegistry);

会话会话= sessionFactory.openSession();
session.beginTransaction();

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

session.getTransaction()。commit();
session.close();
HibernateUtility.getSessionFactory()。close();


$ / code $ / pre

当我运行上面的代码时, 。

  2016年1月18日上午9:34:04 org.hibernate.dialect.Dialect< init> 
产品介绍:HHH000400:使用方言:org.hibernate.dialect.SQLServerDialect并在
异常线程 main org.hibernate.MappingException:未知实体:hibernate.User
在org.hibernate.internal .SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
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(DefaultSaveEven tListener.java:32)美元,org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73 B $ B)
在org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)在org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
在org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)

在hibernate.App .main(App.java:27)

我见过很多人有这个问题,但仍然没有能够弄清楚我的。感谢您的帮助。

解决方案

您的会话工厂配置为对Hibernate 5不正确。如果您使用Hibernate 5,您可以通过这种方式创建会话工厂。

  sessionFactory = new Configuration()。configure()。 buildSessionFactory(); 

在文件 User.hbm.xml中你不应该使用命名空间



http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd $ b

使用



http://www.hibernate.org / b
$ b

改为

Yes, I have seen this question all over the place,but am still not able to figure out what is the issue with my code. Certainly am overlooking something, can't just tell what. I could certainly use a second eye.

Here's my POJO.

package hibernate;

public class User {
    private int id;
    private String name;
    private int total;
    private int goal;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public int getGoal() {
        return goal;
    }
    public void setGoal(int goal) {
        this.goal = goal;
    }
}

Below is my hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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 name="">
  <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=test;</property>
  <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="hibernate.connection.username">sa</property>
  <property name="hibernate.connection.password">admin</property>

  <mapping resource="hibernate/User.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

Here's my User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 18, 2016 12:10:00 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="hibernate.User" table="USER">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="total" type="int">
            <column name="TOTAL" />
        </property>
        <property name="goal" type="int">
            <column name="GOAL" />
        </property>
    </class>
</hibernate-mapping>

Here's the main app putting the above together:

package hibernate;

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

public class App {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure();
        serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

        sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

        Session session = sessionFactory.openSession();
        session.beginTransaction();

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

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

When i run the above, I get the exception below.

Jan 18, 2016 9:34:04 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernate.User
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
    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:679)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
    at hibernate.App.main(App.java:27)

I have seen many people having this issues,but still havent been able to figure out mine. Thanks a bunch for your help.

解决方案

Your session factory configuration is incorrect for Hibernate 5. If you use Hibernate 5, you can create a session factory by this way

sessionFactory = new Configuration().configure().buildSessionFactory();

In the file User.hbm.xml you shouldn't use namespace

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"

Use

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"

instead

这篇关于获取org.hibernate.MappingException:未知实体:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆