這個問題令我瘋狂。基本上,我已經建立了一個到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