【发布时间】:2011-07-13 15:25:07
【问题描述】:
这个问题快把我逼疯了。基本上,我已经设置了一个到 java 类的休眠映射。该类将插入到表中。但这并没有发生。一开始,由于超时,我被锁定了休眠。其次它工作了,然后它没有工作。
供您参考,我使用的是 netbeans 6.9.1 和随附的 Hibernate 3。而且..最奇怪的是,当我在调试模式下运行程序时(你放置断点,程序从一行到另一行缓慢爬行)我可以得出结论,初始化 sessionFactory 大约需要 5 秒。大概这就是被锁的主要原因吧。
我做错了什么?仅供参考,我在 localhost 中为 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吗?您是否尝试过启用休眠 sql 日志以查看它正在执行什么?你试过监控 mysql 查询吗?
-
是的,我可以。事实上,在同一个项目中,有些页面使用普通的、痛苦的 JDBC 编程进行连接。这些页面运行良好且快速。但是当涉及到使用休眠的页面时……发生了这种情况……我一无所知……
-
Jtahlborn:如何启用和查看休眠 sql 日志?
-
INFO: Hibernate: 插入 app_crs_info (COURSE_NAME, DESCRIPTION, LEVEL, ID) 值 (?, ?, ?, ?)。这就是日志所说的。这很奇怪,当我进行调试时,参数被正确解析(即,课程成员变量已相应设置)
标签: java hibernate jsf netbeans jakarta-ee