Выводит null-значения из таблицы

146
16 ноября 2019, 13:40

Не понимаю, почему выводит null при вызове метода в контроллере

Результат в браузере:

{"id":1,"patronymic":null,"address":null,"phone":null,"filiation":null,"surName":null,"firstName":null}

Скрин таблицы:

Класс Agent:

import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
@Table(name="agent")
public class Agent {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id; // id of Agent
    @Column(name = "surname")
    private String surname; // Agent's surname
    @Column(name = "firstname")
    private String firstname; // Agent's firstname
    @Column(name = "patronymic")
    private String patronymic; // Agent's patronymic
    @Column(name = "address")
    private String address; // Agent's address
    @Column(name ="phone")
    private String phone; // Agent's phone
    @ManyToOne
    @JoinColumn(name="filiationid")
    private Filiation filiation; // Composition of Filiation in Agent class
    public Agent(int id, String surname, String firstname, String patronymic, String address, String phone, Filiation filiation) {
        this.id = id;
        this.surname = surname;
        this.firstname = firstname;
        this.patronymic = patronymic;
        this.address = address;
        this.phone = phone;
        this.filiation = filiation;
    }
    public Agent() {
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getSurName() {
        return surname;
    }
    public void setSurName(String surName) {
        this.surname = surName;
    }
    public String getFirstName() {
        return firstname;
    }
    public void setFirstName(String firstName) {
        this.firstname = firstName;
    }
    public String getPatronymic() {
        return patronymic;
    }
    public void setPatronymic(String patronymic) {
        this.patronymic = patronymic;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public Filiation getFiliation() {
        return filiation;
    }
    public void setFiliation(Filiation filiation) {
        this.filiation = filiation;
    }
}

Есть значения, но выводит null. Работаю с MySQL Workbench.

AgentService:

package com.insuranceManagment.InsuranceCompany.services.impls;
import com.insuranceManagment.InsuranceCompany.dao.impls.AgentDAO;
import com.insuranceManagment.InsuranceCompany.model.Agent;
import com.insuranceManagment.InsuranceCompany.services.interfaces.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AgentService implements IAgentService {
    @Autowired
    @Qualifier("agentDAO")
    private AgentDAO agentDAO;
    @Override
    public List<Agent> getAgents() {
        return agentDAO.getAgents();
    }
    @Override
    public Agent getAgentById(int id) {
        Agent obj = agentDAO.getAgentById(id);
        return obj;
    }
    @Override
    public void addAgent(Agent agent) {
        this.agentDAO.addAgent(agent);
    }
    @Override
    public void removeAgent(int id) {
       this.agentDAO.removeAgent(id);
    }
    @Override
    public void updateAgent(Agent agent) {
     this.agentDAO.updateAgent(agent);
    }
}

AgentController:

import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
public class AgentController {
    @Autowired
    @Qualifier("agentService")
    private AgentService agentService;
    @RequestMapping(value = "/api/createagent")
    @Transactional
    public  void createAgent(@Valid @RequestBody Agent agent){
        agentService.addAgent(agent);
    }
    @RequestMapping(value = "/api/updateagent")
    @Transactional
    public void updateAgent(@Valid @RequestBody Agent agent){
        agentService.updateAgent(agent);
    }
    @RequestMapping(value = "/api/removeagent/{id}")
    @Transactional
    public void removeAgent(@PathVariable(value = "id") int id){
        agentService.removeAgent(id);
    }
    @RequestMapping(value = "/api/getagentbyid/{id}")
    @Transactional
    public Agent getAgentById(@PathVariable(value = "id") int id, Model model){
        model.addAttribute("getagentbyid",this.agentService.getAgentById(id));
        return  this.agentService.getAgentById(id);
    }
    @RequestMapping("/api/getagents")
    public List<Agent> getAgents(){
        return agentService.getAgents();
    }

}

AgentDAO:

package com.insuranceManagment.InsuranceCompany.dao.impls;
import com.insuranceManagment.InsuranceCompany.dao.interfaces.IAgentDAO;
import com.insuranceManagment.InsuranceCompany.model.Agent;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
@Repository("agentDAO")
@Component
public class AgentDAO implements IAgentDAO {
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public List<Agent> getAgents() {
//        Query query = entityManager.createQuery("from Agent as agent order by agent.ID");
//        return (List<Agent>) query.getResultList();
        return null;
    }
    @Override
    public Agent getAgentById(int id) {
       return entityManager.find(Agent.class, id);
    }
    @Override
    public void addAgent(Agent agent) {
       entityManager.persist(agent);
    }
    @Override
    public void removeAgent(int id) {
        this.entityManager.remove(getAgentById(id));
    }
    @Override
    public void updateAgent(Agent agent) {
         Agent agentUpdate = this.getAgentById(agent.getId());
         agentUpdate.setSurName(agent.getSurName());
         agentUpdate.setFirstName(agent.getFirstName());
         agentUpdate.setPatronymic(agent.getPatronymic());
         agentUpdate.setAddress(agent.getAddress());
         agentUpdate.setPhone(agent.getPhone());
         entityManager.flush();
    }
}
READ ALSO
Ошибка при попытке открыть COM порт JSSC

Ошибка при попытке открыть COM порт JSSC

В Java новичек, прошу помощи знатоковПытаюсь сделать пересылку данных через COM порт с помощью библиотеки JSSC

110
открытие проекта из Eclipse в Intellij Idea

открытие проекта из Eclipse в Intellij Idea

Проект, который был написан и скомпилирован в EclipseРаботаю в Intellij Idea

135
Отличие WebApplicatIoninitializer от AbstractAnnotationConfigDispatcherServletInitializer

Отличие WebApplicatIoninitializer от AbstractAnnotationConfigDispatcherServletInitializer

Настроить ServletContext в Spring можно создав класс - конфигурацию и :

137
Ошибка: &ldquo;Cannot resolve symbol support&rdquo;

Ошибка: “Cannot resolve symbol support”

Всё было в порядкеВыгрузил проект из битбакета и при импорте:

134