In Java EE 6, in order to create a transaction you need either an EJB (Container Managed Transactions) or manage the transaction boundaries yourself (Bean Managed Transactions). See the linked tutorial.
The easiest way is the first of the two: to implement it, simply move the relevant code in a @Stateless
EJB (make sure your Persistence Context is not extended)
RegisterBean
public class RegisterBean {
@EJB MyService service;
public void register() {
//TODO: validate username and password
User newUser = new User();
newUser.setPassword(password);
newUser.setUserName(name);
service.create(newUser);
}
}
MyService
@Stateless
public class MyService {
@PersistenceContext(unitName="user-unit")
private EntityManager em;
public void create(User newUser) {
em.persist(newUser);
}
}
Useful Links