一个测试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.
实体类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.
解决方案 »
- javax.servlet.ServletException: Class servlets.LoginServlet is not a Servlet
- 数组循环逻辑问题
- 一个关于编码的问题
- 一个JAVA程序题
- getParameter()和getParameterValues()问题
- 请高手帮忙看看getCurrentSession()的问题
- 谁有 Googleapi.jar ?
- tomcat作web server,如何访问weblogic中的ejb?
- 求ftp的例子
- JBuilder7.0中怎样部署和开发Web Services?
- 我用FileUpload上传文件,在Tomcat中可以使用,可是到了weblogic就不能用了
- hibernate lazy延迟加载 不能用
要不你换个方法试一下...
映射文件 最好将类中的属性与表中的列对应起来,例如:
<id name="no" column="no" length="12">
log4j.logger.com.bjsxt = debug <property name="connection.url">jdbc:mysql://localhost/hibernateTest</property>
数据库没有端口号?仔细检查你的映射文件.
比如:
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
要测试的话 应该用Junit 测试啊