Cannot invoke "jakarta.persistence.EntityManager.createQuery(String, java.lang.Class)" because "this.entityManager" is null - java

Good afternoon, I am trying to get the data from my database. My application is with abse in microservices which has the following
This is my class for the database configuration.
#ApplicationScoped
public class DbConfig {
#Inject
#ConfigProperty(name = "db.connection.username")
private String dbUser;
#Inject
#ConfigProperty(name = "db.connection.password")
private String dbPassword;
#Inject
#ConfigProperty(name = "db.connection.url")
private String dbUrl;
#Produces
#ApplicationScoped
public EntityManager entityManager() {
Map<String, String> properties = new HashMap<>();
properties.put("javax.persistence.jdbc.url", dbUrl);
properties.put("javax.persistence.jdbc.user", dbUser);
properties.put("javax.persistence.jdbc.password", dbPassword);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence-books", properties);
return emf.createEntityManager();
}
}
This is my RepositoryImpl class book
#ApplicationScoped
public class BookRepositoryImpl implements BookRepository {
#PersistenceContext
EntityManager entityManager;
#Override
public List<Book> findAll() {
try {
TypedQuery<Book> query = entityManager.createQuery("SELECT b FROM Book b ORDER BY b.id ASC", Book.class);
return query.getResultList();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
..// another CRUD methods
This is my rest class
#ApplicationScoped
#Path("/books")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public class BookRest {
#GET
public List<Book> findAll() {
System.out.println("Buscando todos");
return bookService.findAll();
}
..// another CRUD Methods
}
The problem arises when I start the application through the server class, it starts correctly at localhost:7001, but in order to verify that it is working correctly I need to list the data in the database through localhost:7001/books, which gives me this error Cannot invoke "jakarta.persistence.EntityManager.createQuery(String, java.lang.Class)" because "this.entityManager" is null

Related

change entitymanager datasource in same session

in my session i need to call 2 diffrent procedures which are on db1 and db2. it works fine when i call them separetely but it fails when i call them in same session. it fetches first data correctly but it fails on second call since it looks for the second procedure on db1 although entitiymanager's datasource changed correctly.
what am i missing?
here is code snippet
#Repository
#Transactional
public class DB1Dao {
#PersistenceContext()
private EntityManager entityManager;
#SuppressWarnings("unchecked")
public Model1 getData(String param1) {
.....
}
}
#Repository
#Transactional
public class DB2Dao {
#PersistenceContext()
private EntityManager entityManager;
#SuppressWarnings("unchecked")
public Model2 getData(String param2) {
.....
}
}
#Autowired
private DB1Dao dao1;
#Autowired
private DB2Dao dao2;
#RequestMapping(value = "/inquiry", method = RequestMethod.POST)
public ResponseEntity<Object> inquiryService(#RequestBody InquiryRequest inquiryRequest){
....
Model1 model1 = dao1.getData(param2); // success
....
Model2 model 2 = dao2.getData(param2); // fails since it looks for second procedure on db1
}
You didn't show any persistence-related configuration info, but I'd guess you should differentiate your repositories by properly naming them:
#PersistenceContext(unitName = "db1PersitenceUnitName")
private EntityManager entityManager;

#Inject gives a NullPointer Exception in java EE

I'm trying to create a webservice that gives some results taken through hibernate from the database.
#Path("/book")
public class BookService {
#Inject
private dbController db;
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getBookTitle() {
return "H2G2";
}
#GET
#Path("/users")
#Produces(MediaType.APPLICATION_JSON)
public Response getUsers(){
List<UserEntity> users = db.getUsers();
return Response.ok(users,MediaType.APPLICATION_JSON).build();
}
}
the db variable whenever I call http://localhost/book/users is always null.
The dbController is:
public class dbController {
#Inject
private HibernateUtil util;
public List<UserEntity> getUsers(){
List<UserEntity> result = null;
try{
result = (List<UserEntity>) this.util.createQuery("select e from UserEntity e");
}catch (Exception e){
System.out.println(e.getMessage());
}
return result;
}
}
and the HibernateUtil is:
public class HibernateUtil {
private static final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("NewPersistenceUnit");
private EntityManager entityManager = null;
private void createEntityManager(){
if (this.entityManager==null){
this.entityManager = entityManagerFactory.createEntityManager(); // here is your persistence unit name
}
}
private void closeConnection(){
this.entityManager.close();
this.entityManager = null;
}
public List createQuery(String query) throws Exception{
this.createEntityManager();
List result;
try{
result = this.entityManager.createQuery(query).getResultList();
}catch (Exception e){
throw new Exception(e.getMessage());
}
return result;
}
}
I'm using intellij and I added a break point at the db.getUsers() and set the variable db by adding new dbController(). However, the Intellij gives me the error "Class not loaded : controller.dbController".
The hibernate works for sure...so the problem is not there. It's the first time I'm trying to use Dependency Injection, but I'm not sure what I'm doing wrong.
Thanks
You cannot inject POJO it has to be a Bean. So making it a bean requires the annotations, for example:
#Stateful
#LocalBean
public class dbController {..} // Should be DbController, start with CAPS
and
#Stateful // or maybe #Stateless ?
#LocalBean
public class HibernateUtil {..}
Then when you have a Bean it is not allowed to use static final so change like this is needed:
private EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("NewPersistenceUnit");
But actually the easiest way to get EntityManager is just to inject it also. Like:
#PersistenceContext// maybe also with unit name (unitName = "whatever_the_unit_name_is")
private EntityManager em;

Spring Boot and multiple databases

I've setup a basic spring project with a single database connection.
In the application.properties file I have the database settings:
spring.datasource.url = jdbc:mysql://192.168.1.19/ticket
spring.datasource.username = dbusername
spring.datasource.password = dbpassword
I've created a base DAO class which other DAOs extend:
#Transactional
public class Dao<E> {
#PersistenceContext
private EntityManager entityManager;
private Class<E> entityClass;
public Dao(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void create(E object) {
entityManager.persist(object);
return;
}
public void delete(E object) {
if (entityManager.contains(object)) {
entityManager.remove(object);
} else {
entityManager.remove(entityManager.merge(object));
}
return;
}
#SuppressWarnings("unchecked")
public List<E> getAll() {
return entityManager.createQuery("from " + entityClass.getName()).getResultList();
}
public E get(long id) {
return entityManager.find(entityClass, id);
}
public void update(E object) {
entityManager.merge(object);
return;
}
}
Here's a sample entity that extends the base DAO:
#Repository
public class PersonDao extends Dao<Person> {
public PersonDao() {
super(Person.class);
}
}
Currently this uses a single database, but I need to be able to add a second database, and somehow define in each DAO which datasource to use. Each DAO will only use a single database, so there's no requirement for a DAO to be able to connect to multiple databases.
I've done some research, and that seems to suggest I need to use JdbcTemplate? but I can't seem to find a tutorial that matches my need. Also, at the minute the entityManager is injected into the DAO, but the JdbcTemplate examples I've looked at don't seem to use the entityManager, which is slightly confusing.
database.password1=<password1>
database.url1=jdbc\:mysql\://localhost\:3306/twodbone
database.username1=<username1>
database.password2=<password1>
database.url2=jdbc\:mysql\://localhost\:3306/twodbtwo
database.username2=<username2>
database.driverClassName=com.mysql.jdbc.Driver
In this way you can add the multiple databases and configure both hibernate.cfg.xml file and applicationContext.xml file also..
#Repository
public class FooRepository
{
#PersistenceContext
private EntityManager entityManager;
#Autowired(required = true)
private JdbcTemplate jdbcTemplate;
public void saveFoo(Foo foo)
{
this.entityManager.persist(foo);
}
public List<SomeReportPojo> getSomeReport()
{
return this.entityManager.queryForList("SELECT .. ",SomeProjectPojo.class);
}
}
this.jdbcTemplate should be kept rather than this.entityManager for jdbc templetes
this is simple example

EJB - Unable to execute results from ServiceLayer

When I execute from DAO layer(ProjectDAOImpl), I'am able to see the results from database.
However when I execute from results from Service layer, I'am getting null pointer exceptions.
java.lang.NullPointerException
ProjectServiceImpl.fetchProjects(ProjectServiceImpl.java:25)
ProjectServiceImpl.main(ProjectServiceImpl.java:36)
How can I resolve this issue?
DAO Layer Interface and Class
#Remote
public interface ProjectDAO {
List<Project> fetchProjectDetail();
}
#Stateless
public class ProjectDAOImpl implements ProjectDAO {
private EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("projects");
public ProjectDAOImpl() {
}
public List<Project> fetchProjectDetail() {
List<Project> listProject = new ArrayList<Project>();
listProject =
getEntityManager().createNamedQuery("Project.findProjects").getResultList();
return listProject;
}
private EntityManager getEntityManager() {
return entityManagerFactory.createEntityManager();
}
}
Service Layer Interface and Class
#Remote
public interface ProjectService {
List<Project> fetchProjectDetail();
}
#Stateless
public class ProjectServiceImpl implements ProjectService {
private ProjectDAO projectDAO;
public ProjectServiceImpl() {
}
#EJB
public void setProjectDAO(ProjectDAO projectDAO) {
this.projectDAO = projectDAO;
}
#GET
#Path("/projects")
#Produces(MediaType.APPLICATION_JSON)
public List<Project> fetchProjects() {
return getProjectDAO().fetchProjectDetail();
}
public ProjectDAO getProjectDAO() {
return projectDAO;
}
public static void main(String [] args) {
ProjectServiceImpl projectServiceImpl = new ProjectServiceImpl();
projectServiceImpl.fetchProjects();
}
}

Java EE, CDI - #inject not working; null

I am somewhat new to Java EE (dependency injection) and I can't figure out why #Inject is giving me null, yet InitialContext.doLookup does work.
Here is my bean. It is just a DAO. A wrapper for EntityManager basically
#Stateless
public class PersonManager {
#PersistenceContext("unitName="PersonData")
EntityManager em;
...
}
Here is a REST service, where I am trying to utilize PersonManager:
#Path("/PersonService")
#RequestScoped
public class PersonService {
#Inject private PersonManager manager; //this comes up null
#GET
#Produces("text/html")
public String getAllPersons() {
List<Person> personList manager.findAll(); //null pointer exception, manager null
}
}
Now what is weird is, if I do a lookup on PersonManager, it does work, like this:
#GET
#Produces("text/html")
public String getAllPersons() {
try {
manager = InitialContext.doLookup("java:global/PersonApp/PersonData/PersonManager");
}
catch(Exception e) {
e.printStackTrace();
}
List<Person> personList manager.findAll(); //this works!
}
Any idea why #Inject doesn't work here? I am using an EAR with a WAR and JAR within it like this:
EAR (PersonApp)
--JAR (PersonData - ejb module - contains PersonManager)
--WAR (PersonRest - web module - contains PersonService)
The problem turned out to be adding PersonService as a singleton in the rest application registration.
#ApplicationPath("api")
public class RestApplication extends Application{
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public RestApplication(){
//below line caused #Inject not to work. commented out
//singletons.add(new PersonService());
}
#Override
public Set<Class<?>> getClasses() {
return empty;
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}

Categories

Resources