Injection of dependencies failure

Asked

Viewed 65 times

0

I am new to the world of Hibernate and when I run this application, I get the following exception (error refers to persist EntityManager):

java.lang.NullPointerException
Caused by: java.lang.NullPointerException
    at com.nataniel.api.services.UserService.createUser(UserService.java:28)

It occurs that the injection of dependencies in the method createUser failed. How do I fix this?

User Entity:

package com.nataniel.api.domain;

import org.hibernate.annotations.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="USER")
public class User {
    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "LOGIN")
    private String login;

    @Column(name = "NAME")
    private String name;

    @Column(name = "EMAIL")
    private String email;

    @Column(name = "PASSWORD")
    private String password;

    @Column(name = "CITY")
    private String city;

    @Column(name = "REGION")
    private String region;

    @Column(name = "BIRTHDATE")
    private String birthDate;

    public User() {
    }

    public User(String login, String name, String email, String password, String city, String region, String birthDate) {
        this.login = login;
        this.name = name;
        this.email = email;
        this.password = password;
        this.city = city;
        this.region = region;
        this.birthDate = birthDate;
    }

    // getters and setters
}

DAO ARCHIVE:

package com.nataniel.api.services;
import org.apache.camel.Exchange;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import com.nataniel.api.domain.User;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
@Service("userService")
public class UserService {

    @PersistenceContext
    transient EntityManager entityManager;

    @Transactional
    public String createUser(Exchange exchange) {
        JSONObject userAccountJSON = (JSONObject) exchange.getIn().getHeader("jsonRequest");

        User user = new User();
        user.setLogin(userAccountJSON.getString("login"));
        user.setEmail(userAccountJSON.getString("email"));
        user.setPassword(userAccountJSON.getString("password"));
        user.setName(userAccountJSON.getString("name"));
        user.setCity(userAccountJSON.getString("city"));
        user.setRegion(userAccountJSON.getString("region"));
        user.setBirthDate(userAccountJSON.getString("birthdate"));
        entityManager.persist(user);

        return userAccountJSON.toString();
    }
}

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">

<persistence-unit name="service-provider">

    <!-- provedor/implementacao do JPA -->
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <!-- entidade mapeada -->
    <class>com.nataniel.api.domain.User</class>

    <properties>
        <!-- dados da conexao -->
        <property name="javax.persistence.jdbc.driver"
                  value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url"
                  value="jdbc:mysql://192.168.0.140:3306/service-provider" />
        <property name="javax.persistence.jdbc.user"
                  value="root" />
        <property name="javax.persistence.jdbc.password"
                  value="123" />

        <!--  propriedades do hibernate -->
        <property name="hibernate.dialect"
                  value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />

        <!--  atualiza o banco, gera as tabelas se for preciso -->
        <property name="hibernate.hbm2ddl.auto" value="update" />

    </properties>
</persistence-unit>

  • Did you duplicate the question? https://answall.com/questions/168082/como-resolver-este-java-lang-illegalargumentexception-unknown-entity-ao-rotate

  • @diegofm no, this is about injecting the entityManager

  • @diegofm This question is not duplicate the other. It is very similar, it is very related, but it is not duplicate. See my comments in the other question and also note that in this one, the method createUser is different. I voted to reopen the question and edited it to make it clearer.

  • @Victorstafusa ok, I voted to reopen too, the OP accepted the duplicate, even though he did not agree, I was confused by this.

1 answer

1

Hello.

@Service is used to designate a service class that executes business rules, so it is not a good practice to use in a persistence class. In your class UserService (that I believe should be UserDAO) it is good practice to use the annotation @Repository.

The person responsible for making the injection of EntityManager is the Spring Container. Put the code snippet of the spring configuration so I can analyze.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.