How generate DAO with Hibernate Tools in Eclipse?

我怕爱的太早我们不能终老 提交于 2019-12-04 10:28:59

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!