[英]Hibernate Mapping Exception:Unknown Mapping Entity :Theme
我想將主題類映射到主題表。
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)處(DefaultSaveEventListener.java:56)在org.hibernate處的org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)處。 org.hibernate.impl.SessionImpl.fireSave(Sessio)上的event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) nImpl.java:713)位於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.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl。 java:43),位於com.intellij.rt.execution.application.AppMain.main(AppMain.java:134),位於java.lang.reflect.Method.invoke(Method.java:606)
錯誤是您應該像<class name="my.package.Themes" table="themes">
這樣,將軟件包名稱添加到Themes.hbm.xml
。
另一個問題是您的映射不等於您的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.