如何在 Eclipse 中使用 Hibernate Tools 生成 DAO?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7251124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:56:18  来源:igfitidea点击:

How generate DAO with Hibernate Tools in Eclipse?

javaeclipsehibernatejakarta-eehibernate-tools

提问by Valter Silva

I'm using : Eclipse Java EE IDE Web Developers version:Indigo Release

我正在使用:Eclipse Java EE IDE Web Developers 版本:Indigo Release

with hibernate tools, i'm new to hibernate in Eclipse, so i learn how configure the hibernate and generate the POJO's with annotations (which i think is better than .xml).

使用休眠工具,我是 Eclipse 中休眠的新手,所以我学习了如何配置休眠并生成带有注释的 POJO(我认为这比 .xml 更好)。

So after generate my POJO's and DAO's i try to make a insertion, but launch a 'null point exception' to my entity manager, this is how hibernate tools is generating the dao classes:

因此,在生成我的 POJO 和 DAO 之后,我尝试进行插入,但向我的实体管理器启动了一个“空点异常”,这就是休眠工具生成 dao 类的方式:

Trying to use the DAO generated:

尝试使用生成的 DAO:

public static void main(String[] args) {
// TODO Auto-generated method stub
    User user = new User();
    user.setEmail("[email protected]");
    user.setPassword("123456");
    user.setReputation(0);

    DaoUser daoUser = new DaoUser();
    daoUser.persist(user);
}

DAO generated:

DAO 生成:

package com.example.pojo;

// Generated 30/08/2011 20:43:29 by Hibernate Tools 3.4.0.CR1

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Home object for domain model class User.
 * @see com.example.pojo.User
 * @author Hibernate Tools
 */
@Stateless
public class UserHome {

    private static final Log log = LogFactory.getLog(UserHome.class);

    @PersistenceContext
    private EntityManager entityManager;

    public void persist(User transientInstance) {
        log.debug("persisting User instance");
        try {
            entityManager.persist(transientInstance);
            log.debug("persist successful");
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(User persistentInstance) {
        log.debug("removing User instance");
        try {
            entityManager.remove(persistentInstance);
            log.debug("remove successful");
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public User merge(User detachedInstance) {
        log.debug("merging User instance");
        try {
            User result = entityManager.merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public User findById(Integer id) {
        log.debug("getting User instance with id: " + id);
        try {
            User instance = entityManager.find(User.class, id);
            log.debug("get successful");
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
}

I think i must be doing something wrong in the configuration process. How should I set correctly my classes and dao's ?

我想我一定是在配置过程中做错了什么。我应该如何正确设置我的类和 dao ?

采纳答案by javamonkey79

How are you injecting in your entity manager? By the looks of it, you are trying to run an enterprise application in SE.

你是如何注入你的实体管理器的?从表面上看,您正在尝试在 SE 中运行企业应用程序。

If you really need this to run in SE (hence the "main" method) you'll need to bootstrap the persistence engine somehow.

如果你真的需要它在 SE 中运行(因此是“main”方法),你需要以某种方式引导持久性引擎。

I usually provide a setter to the entity manager or provide an abstract getter. From there you can do something like this:

我通常为实体管理器提供一个 setter 或提供一个抽象的 getter。从那里你可以做这样的事情:

    _entityManagerFactory = Persistence.createEntityManagerFactory( "myJDBC" );
    _entityManager = _entityManagerFactory.createEntityManager();

    UserHome userHome = new UserHome();
    userHome.setEntityManger( _entityManager );

You'll also need a peristence.xml file with a persistence unit matching whatever you end up calling "myJDBC".

您还需要一个 peristence.xml 文件,其中的持久性单元与您最终调用的“myJDBC”相匹配。

I hope this helps.

我希望这有帮助。

EDIT #1

编辑#1

There is an example herethat I think will help you out. It is a helloworld with JPA, Toplink and MySQL, though the MySQL part does not matter, you can switch your driver out if needs be.

有一个例子在这里,我想对您会有帮助。它是一个带有 JPA、Toplink 和 MySQL 的 helloworld,虽然 MySQL 部分无关紧要,但如果需要,您可以切换驱动程序。

EDIT #2

编辑#2

There is also an example herethat uses hibernate only (not so much JPA).

还有一个例子这里是使用Hibernate只(没有这么多JPA)。

EDIT #3

编辑 #3

I think the output from the hibernate tools in the enterprise Eclipse tooling is geared towards that: enterprise java. That being said, it is much easier to take what you have and run it in EE. That isn't to say that you can'trun it in SE, just that it is a little more challenging.

我认为企业 Eclipse 工具中的休眠工具的输出面向:企业 Java。话虽如此,使用您拥有的东西并在 EE 中运行它要容易得多。这并不是说您不能在 SE 中运行它,只是它更具挑战性。

On that note, whenever I use hibernate in SE without JPA, I augment it with Spring - this takes the load off significantly. I wouldn't worry about that until you get it working, but I'd consider looking at it once you've learned a few lessons about hibernate and\or JPA.

在这一点上,每当我在没有 JPA 的情况下在 SE 中使用 hibernate 时,我都会用 Spring 对其进行扩充——这会显着减轻负载。在你让它工作之前我不会担心这个,但是一旦你学习了一些关于 hibernate 和/或 JPA 的课程,我会考虑看看它。