繁体   English   中英

Hibernate映射异常:未知实体:org.hibernate.test.akashtest.UserDetails

[英]Hibernate Mapping Exception : Unknown entity: org.hibernate.test.akashtest.UserDetails

映射类<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;
    }

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM