一个通用的Hibernate DAO

经过one-to-one和one-to-many测试没有问题,看直接复制到任何需要DAO的工程中使用
代码
强烈建议在实际使用中加个接口

BaseDAO.JAVA

package com.lusm.HibernateSessionFactory;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class BaseDAO {
    /** *//**
     * 添加实体
     * @param obj,要添加的实体对象
     * @throws Exception
     */
    public void add(Object obj) throws Exception{
        Session session = null;
        try {
            session = HibernateSessionFactory.getSession();
            session.save(obj);
            session.beginTransaction().commit();
            if(session!=null){
                  session.close();
            }
        } catch (RuntimeException e) {
            session.beginTransaction().rollback();
            if(session!=null){
                  session.close();
            }
            throw e;
        }
    }
   
    /** *//**
     * 删除实体
     * @param obj,要删除的实体
     * @throws Exception
     */
    public void delete(Object obj) throws Exception{
        Session session = null;
        try {
            //取得session对象
            session =HibernateSessionFactory.getSession();
            //删除实体
            session.delete(obj);
            //提交事务
            session.beginTransaction().commit();
            if(session!=null){
                  session.close();
            }
        } catch (Exception e) {
            session.beginTransaction().rollback();//事务回滚
            if(session!=null){
                  session.close();
            }
            throw e;
        }
    }

    /** *//**
     * 更新实体
     * @param obj,要更新的实体
     * @throws Exception
     */
    public void update(Object obj) throws Exception{
        Session session=null;
        try {
            //取得session对象
            session=HibernateSessionFactory.getSession();
            //删除实体
            session.update(obj);
            //提交事务
            session.beginTransaction().commit();
            if(session!=null){
                  session.close();
            }
        } catch (Exception e) {
            session.beginTransaction().rollback();//事务回滚
            if(session!=null){
              session.close();
            }
            throw e;
        }
    }
   
    /** *//**
     * 根据指定的hql进行查询,并返回查询结果
     * @param hql,hql语句
     * @return 查询结果
     * @throws Exception
     */
    public List<?> findByHQL(String hql) throws Exception{
        try {
            Query queryObject =HibernateSessionFactory.getSession().createQuery(hql);
            return queryObject.list();
        } catch (Exception e) {
            throw e;
        }
    }
    /** *//**
     * 根据指定的实体类型和主键的值,查找实体对象
     * @param cls,实体的类
     * @param key,主键的值
     * @return,查找的实体对象
     * @throws Exception
     */
    public Object findById(String cls,Serializable key)
        throws Exception
    {
        try {
            Object instance = (Object) HibernateSessionFactory.getSession().get(cls, key);
            return instance;
        } catch (Exception e) {
            throw e;
        }
       
    }
}
HibernateSessionFactory.java 我就不发了,每个工程里都有

值得注意到是:
       写代码是必须考虑到效率,资源利用,第一,不要创建无谓的实例,第二,不要写没有必要的返回语句,第三,close是有目的的,不可以滥用,第四,必须考虑到哪一步出错的概率高,必须在下一步先做判断。

比如代码:
           /** *//**
     * 添加实体
     * @param obj,要添加的实体对象
     * @throws Exception
     */
    public void add(Object obj) throws Exception{
        Session ses=null;
        Transaction tx=null;
        try {
            //取得session对象
            ses=HibernateSessionFactory.getSession();
            //开始事务
            tx=ses.beginTransaction();
            //保存实体
            ses.save(obj);
            //提交事务
            tx.commit();
        } catch (Exception e) {
            tx.rollback();//事务回滚
            throw e;
        }finally{
            //关闭session
            HibernateSessionFactory.closeSession();
        }
    }
使用one-to-many中执行删除时, 你可以会遇到这样的错误

Exception in thread "main" org.hibernate.TransientObjectException: the detached instance passed to delete() had a null identifier
.........或者
Exception in thread "main" org.hibernate.TransientObjectException: the detached instance passed to delete() had a null identifier
    at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:63)
    at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:761)
    at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:739)
    at com.lusm.HibernateSessionFactory.BaseDAO.delete(BaseDAO.java:44)
    at com.lusm.main.Del.main(Del.java:19)或
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at com.lusm.HibernateSessionFactory.BaseDAO.delete(BaseDAO.java:46)
    at com.lusm.main.Del.main(Del.java:18)
Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`lusm/test1`, CONSTRAINT `test1_ibfk_1` FOREIGN KEY (`id`) REFERENCES `test` (`id`))
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1669)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1085)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
     9 more
原因是你的xml配置和数据库创建有问题
下面给出一个成功的例子
many

xml config
<?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 ">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.lusm.test.Test1" table="test1" catalog="lusm">
        <id name="sid" type="java.lang.Integer">
            <column name="sid" />
            <generator class="increment" />
        </id>
        <many-to-one name="test" class="com.lusm.test.Test" fetch="select" cascade="save-update" >
            <column name="id" not-null="true" />
        </many-to-one>
        <property name="sname" type="java.lang.String">
            <column name="sname" length="20" />
        </property>
    </class>
</hibernate-mapping>
code
package com.lusm.test;

/** *//**
* Test1 generated by MyEclipse Persistence Tools
*/

public class Test1 implements java.io.Serializable {

    // Fields

    private Integer sid;
    private Test test;
    private String sname;

    // Constructors

    /** *//** default constructor */
    public Test1() {
    }

    /** *//** minimal constructor */
    public Test1(Test test) {
        this.test = test;
    }

    /** *//** full constructor */
    public Test1(Test test, String sname) {
        this.test = test;
        this.sname = sname;
    }

    // Property accessors

    public Integer getSid() {
        return this.sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Test getTest() {
        return this.test;
    }

    public void setTest(Test test) {
        this.test = test;
    }

    public String getSname() {
        return this.sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

}
one

xml config
<?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 ">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.lusm.test.Test" table="test" catalog="lusm">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" />
        </property>
        <set name="test1s" inverse="true">
            <key>
                <column name="id" not-null="true" />
            </key>
            <one-to-many class="com.lusm.test.Test1"/>
        </set>
    </class>
</hibernate-mapping>
code
package com.lusm.test;

import java.util.HashSet;
import java.util.Set;

/** *//**
* Test generated by MyEclipse Persistence Tools
*/

public class Test implements java.io.Serializable {

    // Fields

    private Integer id;
    private String name;
    private Set test1s = new HashSet(0);

    // Constructors

    /** *//** default constructor */
    public Test() {
    }

    /** *//** full constructor */
    public Test(String name, Set test1s) {
        this.name = name;
        this.test1s = test1s;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

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

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set getTest1s() {
        return this.test1s;
    }

    public void setTest1s(Set test1s) {
        this.test1s = test1s;
    }

}
db sql
create table `lusm`.`test1`(
`sid` INT not null auto_increment,
`id` INT not null,
`sname` varchar(20),
primary key (`sid`),
index(sid),
foreign key(id) references test(id) ON DELETE CASCADE ON UPDATE CASCADE
);
    create table `lusm`.`test`(
        `id` INT not null auto_increment,
       `name` VARCHAR(20),
        primary key (`id`)
    );
下面给出 该示例的两个测试类

insert
package com.lusm.main;

import com.lusm.HibernateSessionFactory.BaseDAO;
import com.lusm.test.Test;
import com.lusm.test.Test1;


public class Main {

    /** *//**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Test test = new Test();

        test.setName("nihao");
        BaseDAO td = new BaseDAO();
        td.add(test);
       
        Test1 t1 = new Test1(test);
        Test1 t2 = new Test1(test);
        Test1 t3 = new Test1(test);
        t1.setSid(1);
        t2.setSid(2);
        t3.setSid(3);
        t1.setSname("nihao");
        t2.setSname("mfafs");
        t3.setSname("acncs");
       
        BaseDAO td1 = new BaseDAO();
        td1.add(t1);
        td1.add(t2);
        td1.add(t3);
              
    }
}
delete
package com.lusm.main;

import com.lusm.HibernateSessionFactory.BaseDAO;
import com.lusm.test.Test;

public class Del {

    /** *//**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Test test = new Test();
        test.setId(1);
        BaseDAO bd = new BaseDAO();
        bd.delete(test);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值