简体   繁体   中英

Hibernate Mapping Exception:Unknown Mapping Entity :Theme

I want to map class Themes to themes table.

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>

I a reading the contents from a csv file and i want it to insert in a databse using the following code.

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;
}

I am getting the following error

Exception in thread "main" org.hibernate.MappingException: Unknown entity: Themes at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSave(Sessio nImpl.java:713) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697) at ManageData.addTheme(ManageData.java:114) at ManageData.main(ManageData.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

The mistake is you should add your package name into the Themes.hbm.xml like <class name="my.package.Themes" table="themes"> than it works.

Another problem is that your mapping is not equivalent to your getter & setter and fields:

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

text does not exists change it to theme. And the orderInfo getter/setter should looks like:

public int getOrderInfo() {
    return orderInfo;
}

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

Than the theme class works for me.

€dit: You can use something like that, too.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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