如何在Eclipse中使用Hibernate Tools生成DAO? [英] How generate DAO with Hibernate Tools in Eclipse?

查看:288
本文介绍了如何在Eclipse中使用Hibernate Tools生成DAO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:
Eclipse Java EE IDE Web开发人员
版本:Indigo Release



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

所以在生成我的POJO和DAO之后我试图做一个插入,但是对我的实体管理器启动一个空点异常,这是hibernate工具如何生成dao类的:



尝试使用DAO生成:

pre $ public static void main(String [] args){
// TODO自动生成的方法存根
User user = new User();
user.setEmail(valter@brainset.com.br);
user.setPassword(123456);
user.setReputation(0);

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

$ / code>

生成DAO:

  package com.example.pojo; 

//生成30/08/2011 20:43:29通过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;

/ **
*域模型类User的主对象。
* @see com.example.pojo.User
* @author Hibernate Tools
* /
@Stateless
public class UserHome {

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

@PersistenceContext
私人EntityManager entityManager;

public void persist(User transientInstance){
log.debug(persistence User instance);
尝试{
entityManager.persist(transientInstance);
log.debug(坚持成功);
} catch(RuntimeException re){
log.error(persist failed,re);
throw re;



public void remove(User persistentInstance){
log.debug(remove User instance);
尝试{
entityManager.remove(persistentInstance);
log.debug(删除成功);
} catch(RuntimeException re){
log.error(remove failed,re);
throw re;


$ b $公共用户合并(User detachedInstance){
log.debug(合并用户实例);
尝试{
用户结果= entityManager.merge(detachedInstance);
log.debug(merge successful);
返回结果;
} catch(RuntimeException re){
log.error(merge failed,re);
throw re;


$ b $ public User findById(Integer id){
log.debug(正在获取带有id的用户实例:+ id);
尝试{
User instance = entityManager.find(User.class,id);
log.debug(get success);
返回实例;
} catch(RuntimeException re){
log.error(get failed,re);
throw re;



$ / code $ / pre

我认为我必须做的配置过程中出现错误。
我应该如何正确设置我的类和道具?

解决方案

你如何注入实体管理器?从外观上看,你试图在SE运行一个企业应用程序。

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

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

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

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

您还需要一个peristence.xml文件,它的持久性单元与您最终调用的 myBDBC。



我希望这有助于您。



编辑#1 p>

有一个例子这里我认为会帮助你。这是一个带有JPA,Toplink和MySQL的helloworld,尽管MySQL的部分并不重要,但如果需要的话,你可以将你的驱动程序切换出去。

编辑#2



还有一个例子这里只使用hibernate(并不是太多的JPA)。

编辑#3



我认为企业Eclipse工具中的hibernate工具的输出适用于:enterprise java。这就是说,采取你拥有的并且在EE中运行它要容易得多。这并不是说你不能在SE中运行它,只是它有点更具挑战性。

在那个笔记上,每当我在没有JPA的情况下在SE中使用hibernate时,我会用Spring来扩充它 - 这会显着减少负载。我不会为此担心,直到你得到它的工作,但我会考虑看看它,一旦你了解了关于休眠和JPA的一些经验教训。


I'm using : Eclipse Java EE IDE Web Developers version: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).

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:

Trying to use the DAO generated:

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

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

DAO generated:

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 ?

解决方案

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

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

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

    _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".

I hope this helps.

EDIT #1

There is an example here that 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.

EDIT #2

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

EDIT #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't run it in SE, just that it is a little more challenging.

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.

这篇关于如何在Eclipse中使用Hibernate Tools生成DAO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆