我想将“主题”类映射到“主题”表。
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)
错误是您应该将包名称添加到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] 删除。
我来说两句