【问题标题】:Hibernate Mapping Exception : Unknown entity: org.hibernate.test.akashtest.UserDetails休眠映射异常:未知实体:org.hibernate.test.akashtest.UserDetails
【发布时间】:2017-05-28 19:24:43
【问题描述】:

映射类<mapping class="org.hibernate.test.akashtest.UserDetails"/>。 配置文件无法选择 UserDetails 类。 在配置中添加了Annotation 类,以及包位置。

我该如何解决这个问题?

代码:

package org.hibernate.test.akashtest;

import java.lang.annotation.Annotation;
import javax.persistence.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.fasterxml.classmate.AnnotationConfiguration;
import com.fasterxml.classmate.AnnotationInclusion;



public class hibernate_1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        UserDetails user=new UserDetails();
        user.setuserId(1);
        user.setuserName("First User");
        SessionFactory sessionFactory= new Configuration().addAnnotatedClass(org.hibernate.test.akashtest.UserDetails.class).configure("/org/hibernate/test/akashtest/hibernate.cfg.xml").buildSessionFactory();
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();

    }

}

/// My hibernate.cfg.xml
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration
>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">akash5758</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>
        <mapping package="org.hibernate.test.akashtest"/>
        <mapping class="org.hibernate.test.akashtest.UserDetails"/> /*MAPPING CLASS*/

    </session-factory>
</hibernate-configuration>


/// My UserDetails.java
package org.hibernate.test.akashtest;

public class UserDetails {

    private int userId;
    private String userName;
    public  int getUserId()
    {
        return userId;
    }
    public void setuserId(int userId)
    {
        this.userId=userId;
    }
    public String getuserName()
    {
        return userName;
    }
    public void setuserName(String userName)
    {
        this.userName=userName;
    }

}

【问题讨论】:

  • 你的类没有用@Entity注解。它没有@Id。请阅读用户手册。或者,既然您似乎在 Hibernate 团队工作(您的包是 org.hibernate,对吗?),请询问您的同事。

标签: java hibernate hibernate.cfg.xml


【解决方案1】:

您必须在 UserDetails 类中添加 @Entity 注释。

【讨论】:

    猜你喜欢
    • 2017-01-22
    • 1970-01-01
    • 2016-10-18
    • 2019-12-03
    • 2020-12-18
    • 2016-02-05
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多