休眠映射异常:未知的映射实体:主题

anjaneypandey

我想将“主题”类映射到“主题”表。

Themes.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;  
import javax.persistence.Table;
import javax.persistence.Column;

public class Themes
{
private int id;
private String theme;
private int orderInfo;
public Themes(String theme,int order_info)
{
    System.out.println("OK");
    this.theme=theme;
    this.orderInfo=order_info;
}
public int getId()
{
    return id;
}

public void setId(int id)
{
    this.id=id;
}

public String getTheme()
{
    return theme;
}

public void setTheme(String theme)
{
    this.theme=theme;
}

public int getOrder()
{
    return orderInfo;
}

public void setOrder(int order_info)
{
    this.orderInfo=order_info;
}
}

Themes.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="Themes" table="themes">
      <meta attribute="class-description">
     This class contains theme details. 
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="text" column="text" type="string"/>
      <property name="orderInfo" column="order_info" type="int"/>
   </class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/content_templating_data
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">

   </property>

   <!-- List of XML mapping files -->
   <mapping resource="themes.hbm.xml"/>
   <mapping resource="patterns.hbm.xml"/>
   <mapping resource="filler.hbm.xml"/>
   <mapping resource="sentences.hbm.xml"/>

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

我从csv文件中读取内容,并希望使用以下代码将其插入到databse中。

ManageData.java

import java.io.*;

import org.apache.log4j.BasicConfigurator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageData {
private static SessionFactory factory;
private static String csvfile="C:\\Users\\ANJANEY\\IdeaProjects\\hiveminds\\src\\file.csv";
private static String line="";
private static String splitby=",";
private static BufferedReader br=null;

private static SessionFactory getSessionFactory() {
    // create configuration using hibernate API
    Configuration configuration = new Configuration();
    configuration.setProperty("connection.driver_class",
            "com.mysql.jdbc.Driver");
    configuration.setProperty("hibernate.connection.url",
            "jdbc:mysql://localhost:3306/content_templating_data");
    configuration.setProperty("hibernate.connection.username", "root");
    configuration.setProperty("hibernate.connection.password", "");
    return configuration.buildSessionFactory();
}
public static void main(String args[])throws IOException {
    int count=0;
    try
    {
        factory=getSessionFactory();
        System.out.println("Factory Object created...");


    }
    catch (Throwable ex)
    {
        System.out.println("Failed to create Session Factory Object " + ex);
        //throw new ExceptionInInitializerError();
    }

    try {
        int order_info;
        br = new BufferedReader(new FileReader(csvfile));
        ManageData MD = new ManageData();

        line = br.readLine();
        int length=0;
        while ((line = br.readLine()) != null) {
            count++;
            String[] str = line.split(splitby);
            length=str.length;

            order_info = Integer.parseInt(str[2]);

            //Adding theme details in the theme table
            Integer themeID = MD.addTheme(str[1], order_info);


        }
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }finally{
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    System.out.println("Done "+count);
}

//Method to add in theme table
public Integer addTheme(String theme,int order_info){

    Session session = factory.openSession();
    Transaction tx = null;
    Integer themeID = new Integer(0);

    try{
        tx = session.beginTransaction();

        Themes th=new Themes(theme,order_info);
        themeID = (Integer) session.save(th);
        System.out.println("OKAY");
        tx.commit();

    }catch (HibernateException e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace();
    }finally {
        session.close();
    }
    return themeID;
}

我收到以下错误

线程“主”中的异常org.hibernate.MappingException:未知实体:org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485)上org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)的主题在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120) (org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)(org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)处的(DefaultSaveEventListener.java:56)。在org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)的hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)在org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)在org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)在ManageData.addTheme(ManageData.java:114)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在ManageData.main(ManageData.java:66)在)处sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)在com处java.lang.reflect.Method.invoke(Method.java:606)处sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) .intellij.rt.execution.application.AppMain.main(AppMain.java:134)org.hibernate.impl中的SessionImpl.fireSave(SessionImpl.java:713).org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)中的SessionImpl.save(SessionImpl.java:701)在ManageData.addTheme(ManageData)中.java:114)位于sun.reflect.NativeMethodAccessorImpl.invoke的ManageData.main(ManageData.java:66)invoke0(sun.reflect.NativeMethodAccessorImpl.invoke(自然方法)的invoke(NativeMethodAccessorImpl.java:57)位于sun.reflect.DelegatingMethodAccessorImpl。 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)处java.lang.reflect.Method.invoke(Method.java:606)处的invoke(DelegatingMethodAccessorImpl.java:43)org.hibernate.impl中的SessionImpl.fireSave(SessionImpl.java:713).org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)中的SessionImpl.save(SessionImpl.java:701)在ManageData.addTheme(ManageData)中.java:114)位于sun.reflect.NativeMethodAccessorImpl.invoke的ManageData.main(ManageData.java:66)invoke0(sun.reflect.NativeMethodAccessorImpl.invoke(自然方法)的invoke(NativeMethodAccessorImpl.java:57)位于sun.reflect.DelegatingMethodAccessorImpl。 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)处java.lang.reflect.Method.invoke(Method.java:606)处的invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)处的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处的Reflection.NativeMethodAccessorImpl.invoke0(本机方法) com.intellij.rt.execution.application.AppMain.main上的Method.java:606)(AppMain.java:134)在java.lang.reflect.Method.invoke(sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)处的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处的Reflection.NativeMethodAccessorImpl.invoke0(本机方法) com.intellij.rt.execution.application.AppMain.main上的Method.java:606)(AppMain.java:134)

pL4Gu33

错误是您应该将包名称添加到Themes.hbm.xml类似的名称中<class name="my.package.Themes" table="themes">

另一个问题是您的映射不等于您的getter和setter和字段:

<property name="text" column="text" type="string"/>
<property name="orderInfo" column="order_info" type="int"/>

文本不存在,将其更改为主题。而且orderInfo getter / setter应该如下所示:

public int getOrderInfo() {
    return orderInfo;
}

public void setOrderInfo(int order_info) {
    this.orderInfo = order_info;
}

比主题类对我有用。

€dit:您也可以使用类似的方法。

<hibernate-mapping package="my.package">
<class name="Themes" table="themes">
 ....
 </hibernate-mapping>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

休眠:映射映射异常

来自分类Dev

休眠映射抛出异常

来自分类Dev

org.hibernate.MappingException:实体映射中的重复列。休眠映射异常

来自分类Dev

Classcast异常休眠映射nvarchar

来自分类Dev

教义映射到未知实体

来自分类Dev

从4.3.2版起未映射休眠实体

来自分类Dev

休眠实体中的映射外键

来自分类Dev

通过Java配置映射休眠表实体

来自分类Dev

休眠映射异常-无法确定以下类型:

来自分类Dev

休眠映射

来自分类Dev

JPA映射通过引用未知目标实体

来自分类Dev

在非实体java bean中映射多个休眠实体

来自分类Dev

映射到实体时的JPA异常

来自分类Dev

休眠,从映射文件生成实体和数据库

来自分类Dev

休眠,复杂关系中的实体类映射

来自分类Dev

休眠Oracle-一些实体表未映射

来自分类Dev

通过中间表使用 ManyToOne 休眠映射实体

来自分类Dev

在休眠中使用@OneToMany或@ManyToMany定位未映射的类异常

来自分类Dev

休眠多对多映射持久性异常

来自分类Dev

使用@OneToMany或@ManyToMany定位休眠中的未映射类异常

来自分类Dev

创建休眠映射XML文件时遇到Nullpointer异常?

来自分类Dev

休眠(映射)ClassNotFoundException

来自分类Dev

关联表休眠映射

来自分类Dev

sqlserver休眠日期映射

来自分类Dev

指定休眠映射文件

来自分类Dev

休眠未映射

来自分类Dev

@ManyToMany休眠映射问题

来自分类Dev

休眠列表映射

来自分类Dev

映射对象休眠