Injection dependency failure [duplicate]

0

I'm new to the Hibernate world and when I run this application, I get the following exception (error is relative to EntityManager persist):

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

The injection of dependencies in the createUser method failed. How do I fix this?

Entity User:

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 file:

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>

    
asked by anonymous 27.11.2016 / 03:27

1 answer

1

Hello.

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

Spring Container is responsible for injecting EntityManager . Put the code snippet from the spring configuration so I can parse it.

    
27.11.2016 / 04:26