how2j.cn


相关下载
文件名 文件大小
struts_hibernate.rar 9m
使用站长秘制下载工具
步骤 1 : Hibernate准备工作   
步骤 2 : 创建数据库how2java   
步骤 3 : pojo   
步骤 4 : Product.hbm.xml   
步骤 5 : hibernate.cfg.xml   
步骤 6 : dao   

步骤 1 :

Hibernate准备工作

这部分准备Hibernate方面的事情
步骤 2 :

创建数据库how2java

create database how2java
create database  how2java
步骤 3 :

pojo

Product有id,name,price3个字段
package com.how2java.pojo; public class Product { private int id; private String name; private float price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }
步骤 4 :

Product.hbm.xml

映射Product类对应product_表
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.how2java.pojo"> <class name="Product" table="product_"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" /> <property name="price" /> </class> </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.how2java.pojo">
	<class name="Product" table="product_">
		<id name="id" column="id">
			<generator class="native">
			</generator>
		</id>
		<property name="name" />
		<property name="price" />
	</class>
	
</hibernate-mapping>
步骤 5 :

hibernate.cfg.xml

hibernate基本配置,指定数据库,账号,密码,方言等等
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/how2java?characterEncoding=GBK</property> <property name="connection.username">root</property> <property name="connection.password">admin</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/how2java/pojo/Product.hbm.xml" /> </session-factory> </hibernate-configuration>
步骤 6 :

dao

为ProductDAO准备增,删,修改,查询,获取方法
package com.how2java.dao; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.how2java.pojo.Product; public class ProductDAO { public void add(Product p) { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); s.beginTransaction(); s.save(p); s.getTransaction().commit(); s.close(); sf.close(); } public Product get(int id) { Product result = null; SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); result = (Product) s.get(Product.class, id); s.close(); sf.close(); return result; } public void delete(int id) { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); s.beginTransaction(); Product p = (Product) s.get(Product.class, id); s.delete(p); s.getTransaction().commit(); s.close(); sf.close(); } public void update(Product p) { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); s.beginTransaction(); s.update(p); s.getTransaction().commit(); s.close(); sf.close(); } public List<Product> listProduct() { List<Product> result = new ArrayList(); SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session s = sf.openSession(); Query q = s.createQuery("from Product p"); result = q.list(); s.close(); sf.close(); return result; } }


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


问答区域    
2017-12-18 站长大大hibernate创建数据库无法插入数据
canlook9419



按你的方法使用hibernate创建表 插入数据报错 could not insert: [com.sbs.hspa.entity.Student]
entity配置
<hibernate-mapping package="com.sbs.hspa.entity">

    <class name="com.sbs.hspa.entity.Student" table="student_">
        <id name="id" column="id">

            <generator class="native"></generator>
        </id>
        <property name="name" />
        <property name="grade" />
        <property name="performance" />
    </class>
</hibernate-mapping>

entity类
    private int id;
    private String name;
    private String grade;
    private double performance;
HTTP Status 500 - could not insert: [com.sbs.hspa.entity.Student]

type Exception report

message could not insert: [com.sbs.hspa.entity.Student]

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.hibernate.exception.GenericJDBCException: could not insert: [com.sbs.hspa.entity.Student]
	org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
	org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
	org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
	org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)
	org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)
	org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
	org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
	org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
	org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
	org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
	org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
	org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
	org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
	org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
	org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
	org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
	org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
	org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
	com.sbs.hspa.dao.DAO.addData(DAO.java:33)
	com.sbs.hspa.action.StudentAction.addStudent(StudentAction.java:41)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)






答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2017-12-13 SessionFactory 问题
canlook9419
SessionFactory 可以写成一个单例吗 可以的话是不是不用关掉了








答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2017-09-02 是什么导致我查询出的结果错误
2017-09-02 为什么导入Iterator?
2017-08-25 hibernate需要创建对应的表吗
2017-07-15 SessionFactory
2017-06-26 dao里增加删除更新方法里怎么多出一个集合




提问之前请登陆
关于 JAVA 框架-Struts+Hibernate-hibernate 部分 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 389538688
站长会在每个工作日早上尽量回答提问(如果有漏掉没有回答的,请进群提醒一下)
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
站长是玻璃心,提问的时候请语气温柔些 kiss~
截止2017-5-19日累计提问 1638个,站长回答了 1546个
截止2017-8-15日累计提问 2788个,站长回答了 2544个

上传截图