我想将课程主题映射到主题表。
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文件中读取内容,并希望使用以下代码插入数据库中。
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;
}
我收到以下错误
线程“main”中的异常org.hibernate.MappingException:未知实体:主题 at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) 在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) 在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) 在org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50) 在org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 在org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713) 在org.hibernate.impl.SessionImpl.save(SessionImpl.java:701) 在org.hibernate.impl.SessionImpl.save(SessionImpl.java:697) 在ManageData.addTheme(ManageData.java:114) 在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) 在com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
答案 0 :(得分:3)
错误的是你应该将你的包名称添加到Themes.hbm.xml
<class name="my.package.Themes" table="themes">
而不是<property name="text" column="text" type="string"/>
<property name="orderInfo" column="order_info" type="int"/>
。
另一个问题是你的映射不等同于你的getter&amp; setter和fields:
public int getOrderInfo() {
return orderInfo;
}
public void setOrderInfo(int order_info) {
this.orderInfo = order_info;
}
文本不存在将其更改为主题。 orderInfo getter / setter应如下所示:
<hibernate-mapping package="my.package">
<class name="Themes" table="themes">
....
</hibernate-mapping>
比主题课适合我。
€dit:你也可以使用类似的东西。
{{1}}