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 ?
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.