这个问题令我疯狂。基本上,我已经建立了一个到Java类的hibernate映射。该类将插入到表中。但是这并没有发生。一开始,由于超时,我正在将Hibernate锁定。其次,它的工作,然后它没有工作。Hibernate与MySQL插入查询不一致
为了您的信息,我正在使用netbeans 6.9.1和Hibernate 3。最奇怪的是,当我在调试模式下运行程序时(其中放置了断点,并且程序逐渐慢慢地爬行),我可以得出结论,初始化sessionFactory需要大约5秒。这可能是锁定的主要原因。
我做错了什么? Fyi,我在本地主机上使用MySQL的LAMPP。
这里是Hibernate配置XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/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>
<property name="hibernate.connection.url">jdbc:mysql://localhost/rainbow</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这里是XML映射
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="datamap.Course" table="app_crs_info">
<id name="id" column="ID" type="string"/>
<property name="courseName" type="string">
<column name="COURSE_NAME"/>
</property>
<property name="description" type="string">
<column name="DESCRIPTION"/>
</property>
<property name="level" type="integer">
<column name="LEVEL"/>
</property>
</class>
</hibernate-mapping>
这是映射类:
public class Course {
private String id;
private String courseName;
private String description;
private int level;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the courseName
*/
public String getCourseName() {
return courseName;
}
/**
* @param courseName the courseName to set
*/
public void setCourseName(String courseName) {
this.courseName = courseName;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the level
*/
public int getLevel() {
return level;
}
/**
* @param level the level to set
*/
public void setLevel(int level) {
this.level = level;
}
}
这是我怎么把它称为:
public void registerCourse(String id, String description,
String name, String level) {
session = null;
crashLog = new CrashLog();
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
//session.getTransaction().begin();
Course course = new Course();
course.setId(id);
course.setCourseName(name);
course.setDescription(description);
course.setLevel(Integer.parseInt(level));
session.save(course);
//session.getTransaction().commit();
} catch (Exception ex) {
crashLog.writeToLog(CourseData.class.getName() + "Error : " + ex.toString());
} finally {
session.flush();
session.close();
}
}
任何想法家伙?
你能连接到MySQL井外的冬眠?你有没有尝试启用hibernate sql日志来查看它正在执行什么?你有没有尝试监视MySQL查询? – jtahlborn 2011-03-11 17:01:54
是的,我可以。事实上,在同一个项目中,有些页面使用正常的,痛苦的JDBC编程进行连接。这些网页工作正常,速度很快。但是当它涉及到使用休眠时间的页面时,发生这种情况......我太无能了...... – 2011-03-11 17:13:19
Jtahlborn:如何启用并查看hibernate sql日志? – 2011-03-11 17:14:32