一个测试hibernate的小程序,把一个employee对象写入mysql。
实体类package com.sari.model;public class Employee {
private String no;
private String name;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}package process;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;import com.sari.model.Employee;public class EmployeeTest { public static void main(String[] args) {
System.out.println("fdsaf");
Configuration conf=new Configuration();
conf.configure();
ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
SessionFactory sf=conf.buildSessionFactory(sr);
Session sess=sf.openSession();
Transaction tx=sess.beginTransaction();
Employee employee=new Employee();
employee.setNo("12");
employee.setName("曹操");
sess.save(employee);
tx.commit();
sess.close();
sf.close();

}}映射文件<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sari.model">
<class name="Employee" table="employee">
<id name="no">
<generator class="identity"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>配置文件<?xml version='1.0' encoding='GBK'?>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernateTest</property>
<property name="connection.username">root</property>
<property name="connection.password">1111</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/sari/model/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>运行不出结果,数据库显示没写进去,控制台显示警告:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
Hibernate Commons Annotations {4.0.1.Final}
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

解决方案 »

  1.   

    这些都是 log4j的警告 ,不影响 程序
      

  2.   

    没看到错误,log4j 警告而已
      

  3.   

    Action中可以用main方法吗?
    要不你换个方法试一下...
      

  4.   

            <property name="connection.url">jdbc:mysql://localhost/hibernateTest</property>这句应该有问题
      

  5.   

    这句有什么问题?我建的数据库名字叫hibernateTest啊
      

  6.   

    你映射文件中,每个属性怎么没有对应的数据库行呢?<property name="" column=""/>
      

  7.   

    看着确实有点问题:应该加上端口号jdbc:mysql://127.0.0.1:3306/hibernateTest
    映射文件 最好将类中的属性与表中的列对应起来,例如:
    <id name="no" column="no" length="12">
      

  8.   

    首先是你的log4j包的警告的错误,你可以将log4j更改一下级别的,在程序开发用debug,程序部署上线了,改成warn级别(警告级别).
    log4j.logger.com.bjsxt = debug  <property name="connection.url">jdbc:mysql://localhost/hibernateTest</property>
    数据库没有端口号?仔细检查你的映射文件.
    比如:
     <id name="id">
          <generator class="native"/>
        </id>
        <property name="name"/>
      

  9.   

    这是没配置log4j配置文件出的警告
      

  10.   

    我想知道你如何启动这个EmployeeTest程序,  能右键Run as?
    要测试的话 应该用Junit 测试啊
      

  11.   

    这是没配置log4j配置文件出的警告log4j的配置文件是JAVA属性文件的格式,“键=值”文件后缀名.properties
      

  12.   

    这是你的全部错误吗,你配置文件里应该会打印sql语句,但是没有打印,单刀配置文件都没有读取?