如何在 Eclipse 中使用 Hibernate Tools 生成 DAO?
我正在使用: Eclipse Java EE IDE Web 开发人员 版本:带有 hibernate 工具的 Indigo Release
,我是 Eclipse 中的 hibernate 新手,所以我学习如何配置 hibernate 并使用注释生成 POJO(我认为这比 .xml 更好)。
因此,在生成 POJO 和 DAO 后,我尝试进行插入,但向我的实体管理器启动“空点异常”,这就是 hibernate 工具生成 dao 类的方式:
尝试使用生成的 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 生成:
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;
}
}
}
我认为我一定是在配置过程中做错了什么。 我应该如何正确设置我的类和 dao ?
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("[email protected]");
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您如何注入实体管理器?从表面上看,您正在尝试在 SE 中运行企业应用程序。
如果您确实需要在 SE 中运行它(因此是“main”方法),您将需要以某种方式引导持久性引擎。
我通常为实体管理器提供一个 setter 或提供一个抽象 getter。从那里您可以执行以下操作:
您还需要一个 peristence.xml 文件,其中的持久性单元与您最终调用的“myJDBC”相匹配。
我希望这有帮助。
编辑#1
有一个示例这里,我认为这会对您有所帮助。这是一个带有 JPA、Toplink 和 MySQL 的 helloworld,虽然 MySQL 部分并不重要,但如果需要,您可以切换驱动程序。
编辑#2
还有一个示例这里仅使用hibernate(没有那么多JPA)。
编辑 #3
我认为企业 Eclipse 工具中的 hibernate 工具的输出是面向以下目标的: enterprise java.util.话虽这么说,在 EE 中使用现有的东西并运行它要容易得多。这并不是说您不能在SE 中运行它,只是它更具挑战性。
就这一点而言,每当我在没有 JPA 的 SE 中使用 hibernate 时,我都会使用 Spring 来增强它 - 这会显着减轻负载。在你让它工作之前我不会担心这个,但是一旦你学到了一些关于 hibernate 和/或 JPA 的课程,我会考虑看看它。
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:
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.