1

我是休眠的新手。我正在尝试使用 Hibernate 制作一个简单的数据库驱动应用程序。但我被困住了。我什至不能运行一个简单的数据库查询。我收到此错误:

运行程序时生成的日志文件

休眠.cfg.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.DerbyDialect</property>
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/dbtest </property>
    <property name="hibernate.connection.username">root</property>
    <property name="connection.password">pass</property>
    <property name="hibernate.connection.autocommit">false</property>
     <property name="current_session_context_class">thread</property>
    <property name = "show_sql">true</property>
    <property name = "format_sql">true</property>
    <property name = "hibernate.transaction.factory_class">org.hibernate.testing.cache.CachingRegionFactory     
</property>
    <property name = "hibernate.hbm2ddl.auto">create</property>
    <mapping resource="com/enroll/hibernate.hbm.xml"></mapping>
    <mapping class = "com/enroll/insert"></mapping>    
  </session-factory>
</hibernate-configuration>

休眠.hbm.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 catalog="user" name="com.enroll.UserDetail" table="users"/>
  <id column="id" name="id" type="java.lang.Integer">
    <generator class="assigned"/>
  </id>
  <property column="uname" name="uname" type="string"/>
   <property column="pword" name="pword" type="string"/>   
</hibernate-mapping>

用户详细信息.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enroll;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetail {
   @Id private int id;
    String uname;
    String pword;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPword() {
        return pword;
    }

    public void setPword(String pword) {
        this.pword = pword;
    }
}

插入.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.enroll;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class insert {
    public static void main(String[] args){
       UserDetail user1 = new UserDetail();
       user1.setId(7);
       user1.setUname("shri");
       user1.setPword("password");


       SessionFactory sf = (SessionFactory) new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
       Session session = sf.openSession();
       session.beginTransaction();
       session.save(user1);
       session.getTransaction().commit();       
    }
}  

Can any one please tell me where I am wrong and what I should do? 

  [1]: http://i.stack.imgur.com/YbJxA.png
4

3 回答 3

0

您的休眠配置文件在这一行错误:。请删除它,原因:

  1. 它不是带有注释的持久单元
  2. com/enroll/insert 如果正确必须是 com.enroll.insert Java 类也应该以大写开头。你可以查看这篇文章来做快速入门休眠
于 2015-03-14T06:53:34.403 回答
0

您需要将实体属性包含在映射配置文件中的类标记定义中

<class catalog="user" name="com.enroll.UserDetail" table="users">
     <id column="id" name="id" type="java.lang.Integer">
        <generator class="assigned"/>
     </id>
    <property column="uname" name="uname" type="string"/>
    <property column="pword" name="pword" type="string"/>
</class>
于 2015-03-04T16:35:16.850 回答
0
Your hibernate.hbm.xml should be

    <?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 catalog="user" name="com.enroll.UserDetail" table="users">
      <id column="id" name="id" type="java.lang.Integer">
        <generator class="assigned"/>
      </id>
      <property column="uname" name="uname" type="string"/>
       <property column="pword" name="pword" type="string"/>
</class>
    </hibernate-mapping>
于 2015-03-04T16:38:50.893 回答