开发者

Hibernate Inconsistency With MySQL Insert Query

开发者 https://www.devze.com 2023-02-16 02:21 出处:网络
This problem is driving me insane. Basically, I have setup a hibernate mapping to a java class. The class will do insert to a table. But that is not happening. At the beginning, I am getting Hibernate

This problem is driving me insane. Basically, I have setup a hibernate mapping to a java class. The class will do insert to a table. But that is not happening. At the beginning, I am getting Hibernate locked due to timeout. Secondly it worked and then it didnt work.

For your information, I am using netbeans 6.9.1 and Hibernate 3 that came with it. And..the most weird thing is, when I ran the program in the debug mode, (where u put breakpoints, and the program crawl slowly from line to line) I can conclude that it takes around 5 second JUST to initialize the sessionFactory. Probably that is the main reason of the locked up.

What did I do wrong? Fyi, I am using LAMPP for mysql in the localhost.

Here is the Hibernate Configuration 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>
    <p开发者_开发百科roperty name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">password</property>
    <mapping resource="hibernate.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

And here is the mapping 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>

This is the mapped class:

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;
    }
}

And this is how I called it:

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();
        }


    }

Any ideas guys?


Firstly , you uncomment the source to do the transaction demarcation , so there are no transaction defined inside in your code . Please uncomment the line session.getTransaction().begin(); and session.getTransaction().commit();

Moreover , there is no point to do flush() in your finally block . Flushing but never commit after that means all your changes made during flushing will not be saved to the database. The flushing behavior can be control using session.setFlushMode(). By default , it is FlushMode.AUTO, which hibernate will automatically do flushing when a transaction is committed (session.getTransaction().commit();) , so you can remove the line session.flush();

Furthermore, one SessionFactory should represent one database .Creating a SessionFactory is expensive , but creating a session is extremely inexpensive. So you should create one SessionFactory instance and use this single SessionFactory to create all Session throughout your application.

Netbean 's built-in hibernate tool can create a utility class to get a single SessionFactory instance. Please refer to Creating the HibernateUtil.java Helper File at http://netbeans.org/kb/docs/web/hibernate-webapp.html

Hibernate Inconsistency With MySQL Insert Query

`

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号