我一直在尝试使用hibernate来读取和写入mysql数据库,但是当我尝试使用session.save()将dept对象保存到dept mysql表时,我一直收到一个Unknown实体错误。我一直在寻找解决方案的日子,我尝试了很多不同的东西,包括注释我的代码,更改映射xml文件,hibernate配置xml文件等。大多数其他人得到这个错误是使用spring但我不是,所以很多其他答案都不适用。我觉得我失踪了一些小事。
启动器类:
$ rake assets:precompile RAILS_ENV=production
Dept.java:
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
try {
SessionFactory sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
System.out.println("sessionfactory made, now creating session");
Session session = sessionFactory.openSession();
System.out.println("made session");
session.beginTransaction();
System.out.println("making first dept");
session.save( new Dept(1, "Math", "CORE"));
session.save( new Dept(2, "Science", "CORE"));
System.out.println("commiting transaction");
session.getTransaction().commit();
session.close();
System.out.println("SUCCESS");
}
catch (Exception e) {
// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
// so destroy it manually.
e.printStackTrace();
StandardServiceRegistryBuilder.destroy( registry );
System.out.println("exception, destroyed registry");
}
hibernate.cfg.xml中:
package org.myschool.timeline.Timeline_App.domain;
// Generated Jan 16, 2016 1:48:30 AM by Hibernate Tools 4.3.1.Final
import java.util.HashSet;
import java.util.Set;
/**
* Dept generated by hbm2java
*/
public class Dept implements java.io.Serializable {
private int deptId;
private String name;
private String type;
private Set<Course> courses = new HashSet<Course>(0);
public Dept() {
}
public Dept(int deptId, String name, String type) {
this.deptId = deptId;
this.name = name;
this.type = type;
}
public Dept(int deptId, String name, String type, Set<Course> courses) {
this.deptId = deptId;
this.name = name;
this.type = type;
this.courses = courses;
}
//Getters and Setters cut for brevity
}
和Dept.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">pass</property>
<property name="hibernate.connection.url">jdbc:mysql://serverip</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.validator.apply_to_ddl">false</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<mapping resource="org/myschool/timeline/Timeline_App/domain/Admin.hbm.xml" />
<mapping resource="org/myschool/timeline/Timeline_App/domain/Course.hbm.xml" />
<mapping resource="org/myschool/timeline/Timeline_App/domain/Teacher.hbm.xml" />
<mapping resource="org/myschool/timeline/Timeline_App/domain/Dept.hbm.xml" />
<mapping resource="org/myschool/timeline/Timeline_App/domain/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
这是我启动时遇到的错误:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 16, 2016 1:52:21 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="org.myschool.timeline.Timeline_App.domain.Dept" table="DEPT">
<id name="deptId" type="int">
<column name="DEPTID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="type" type="java.lang.String">
<column name="TYPE" />
</property>
<set name="courses" table="COURSE" inverse="false" lazy="true">
<key>
<column name="DEPTID" />
</key>
<one-to-many class="org.myschool.timeline.Timeline_App.domain.Course" />
</set>
</class>
</hibernate-mapping>
完整输出:
making first dept
org.hibernate.MappingException: Unknown entity: org.myschool.timeline.Timeline_App.domain.Dept
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1479)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at org.myschool.timeline.Timeline_App.App.main(App.java:120)
17/01/2016 02:10:41.860 [main] INFO o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000030: Cleaning up connection pool
17/01/2016 02:10:41.862 [main] DEBUG o.h.b.r.i.BootstrapServiceRegistryImpl - Implicitly destroying Boot-strap registry on de-registration of all child ServiceRegistries
exception, destroyed registry
我需要做些什么来解决这个问题并能够将对象保存到数据库?
答案 0 :(得分:1)
修正了,我不得不以不同的方式使sessionfactory,如此
private static SessionFactory buildSessionFactory() {
try{
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sess = configuration.buildSessionFactory(ssrb.build());
return sess;
} catch (Throwable ex) {
System.err.println("SessionFactory creation failed." + ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
我不完全确定为什么会这样,但我猜它不是以其他方式读取hibernate.cfg.xml