Hibernate DAO 实现

发布于 2024-07-17 13:01:00 字数 203 浏览 3 评论 0原文

任何人都可以建议 Web 应用程序的 DAO 实现吗?

如果我为基本操作创建事务(例如 findByID()findALL()createObject()>deleteObject() 等)?

请建议一个支持惰性操作的 DAO 实现。

Can anyone suggest a DAO implementation for a web application?

What will be the problem if I create a transaction for fundamental operation (e.g. findByID(), findALL(), createObject(), deleteObject(), etc.)?

Please suggest a DAO implementation that supports lazy operations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-07-24 13:01:00

如果您使用 Hibernate Tools 生成代码,则会自动为您生成基本的 DAO。 您可以在它们的基础上进行构建。

无论如何,我用于交易的一些代码片段:

public void executeTransaction(Object[] parameters, Transact transact) throws ApplicationException
{
    Transaction tx = null;
    try
    {
        tx = HibernateSessionFactory.getSession().beginTransaction();
        transact.execute(parameters, tx);
        tx.commit();
        LOG.trace("executeTransaction() success");
    }
    catch (Exception e)
    {
        rollback(tx);
        throw new ApplicationException(e);
    }
}

private void rollback(Transaction tx) throws ApplicationException
{
    LOG.warn("rollback()");
    if (tx != null)
    {
        try
        {
            tx.rollback();
        }
        catch (Exception ex)
        {
            LOG.error("rollback() failure",ex);                
        }
    }
}

public interface Transact
{
    public void execute(Object[] parameters, Transaction tx) throws Exception;
}

void updateDistrictImpl(final Distretto district) throws ApplicationException, ApplicationValidationException
{    
try
{
    LOG.trace("updateDistrict[" + distrettoToString(district) + "]");

    executeTransaction(new Transact() {
        public void execute(Object[] parameters, Transaction tx) throws ApplicationException
        {
            DistrettoHome DistrettoDAO = new DistrettoHome();
            DistrettoDAO.attachDirty(district);
        }
        });
    LOG.info("updateDistrict[" + distrettoToString(district) + "] success!");
}
catch (ApplicationException e)
{
    LOG.error("updateDistrict() exception: " + e.getLocalizedMessage(), e);
    throw e;
}
}

If you use Hibernate Tools to generate your code the basic DAOs will be automatically generated for you. You can build upon them.

Anyway, some code snippet I use for transaction:

public void executeTransaction(Object[] parameters, Transact transact) throws ApplicationException
{
    Transaction tx = null;
    try
    {
        tx = HibernateSessionFactory.getSession().beginTransaction();
        transact.execute(parameters, tx);
        tx.commit();
        LOG.trace("executeTransaction() success");
    }
    catch (Exception e)
    {
        rollback(tx);
        throw new ApplicationException(e);
    }
}

private void rollback(Transaction tx) throws ApplicationException
{
    LOG.warn("rollback()");
    if (tx != null)
    {
        try
        {
            tx.rollback();
        }
        catch (Exception ex)
        {
            LOG.error("rollback() failure",ex);                
        }
    }
}

public interface Transact
{
    public void execute(Object[] parameters, Transaction tx) throws Exception;
}

void updateDistrictImpl(final Distretto district) throws ApplicationException, ApplicationValidationException
{    
try
{
    LOG.trace("updateDistrict[" + distrettoToString(district) + "]");

    executeTransaction(new Transact() {
        public void execute(Object[] parameters, Transaction tx) throws ApplicationException
        {
            DistrettoHome DistrettoDAO = new DistrettoHome();
            DistrettoDAO.attachDirty(district);
        }
        });
    LOG.info("updateDistrict[" + distrettoToString(district) + "] success!");
}
catch (ApplicationException e)
{
    LOG.error("updateDistrict() exception: " + e.getLocalizedMessage(), e);
    throw e;
}
}
过去的过去 2024-07-24 13:01:00

有 3 个主要选项:

1) 在映射和查询中正确配置延迟加载:这并不总是最简单的方法,因为在开发 DAO 时,您并不总是知道对象将如何在表示层中使用。

2)使用OpenSessionInView模式:这样,您将能够延迟加载表示层中的相关对象。 这可能是最简单的方法,因为它只需要一点配置。 但要小心,因为延迟加载可能会非常昂贵,并且如果您在表示层中做了一些可疑的事情,您可能会遇到性能问题。 您还可以从演示文稿中修改对象,这意味着更容易犯预先编程错误。

3) 添加一个服务层,将 Hibernate 对象转换为值对象:这是最细粒度的选项,因为您还可以限制向演示文稿公开的 wchi 属性。 您可以在服务调用周围保留事务边界,因此您可以确保在服务之外不会修改任何内容。

在所有情况下,您至少应该尝试正确配置延迟加载。 否则你可能会遇到性能问题!

There are 3 main options :

1) Configure correctly lazy loading in your mappings and in your queries: This is not always the easiest way as you dont always know how your objects will be used in the presentation layer when you develop the DAO.

2) Use the OpenSessionInView pattern: This way, you will be able to lazy load related objects in the presentation layer. This is probably the easiest way as it only require a little bit of configuration. But be careful as lazy loading can be quite expensive and if you do something fishy in your presentation layer you can run into performance problems. You can also modify your objects from the presentation which means it is easier to make preogramming errors.

3) Add a service layer that convert your Hibernate object to value objects: this is the most fine grained option as you can also restrict wchi properties are exposed to the presentation. You keep transaction boundaries around the service call, so you are sure that nothing will be modified outside of a service.

In all cases, you should at least try to configure lazy loading correctly. Else you will probably run into performance problems !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文