I'm trying to post a report object with springBoot and Spring data JPA, but I need to send the report object without one column report_request
is there any way I can send the object without this attribute from the model without removing the atribute report_request
from the class model?
I'm sending it without providing any value but in the data base it is being received as null or empty string, but I need not to send it, as the data base will populate the field automatically with the current date and specific format.
Here is my code:
Model:
package com.ssc.test.cb3.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
/**
* Class that models the entity report request table of the database
* @author ssc
*/
@Entity
@Table(name = "report_request")
@Data
public class ReportRequest {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "seq_id")
private int id;
// Este es el campo en cuestion
@Column(name = "request_date", nullable = true)
private String requestDate;
@Column(name = "request_userid")
private int requestUserId;
@Column(name = "request_email")
private String requestEmail;
@Column(name = "start_date")
private String startDate;
@Column(name = "end_date")
private String endDate;
@Column(name = "report_type")
private int reportType; // 0 === cliente, 1 === proveedor
@Column(name = "contact_id") // Id from the client or provider chosen
private int contactId;
private String rgids;
private int status; // 0 === active, 1 === inactive
@Column(name = "process_start")
private String processStart;
@Column(name = "process_finish")
private String processFinish;
@Column(name = "call_filter") // 0 === Answered calls, 1 === Not answered, 2 === both
private int callQuery;
}
Repository:
package com.ssc.test.cb3.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.ssc.test.cb3.model.ReportRequest;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
* Class that extends to the repository for database management queries with table
report_request
* @author ssc
*/
@Repository
public interface ReportRequestRepository extends JpaRepository<ReportRequest, Integer> {
@Query(
value = "SELECT * FROM report_request WHERE request_userid = :userId",
nativeQuery = true)
List<ReportRequest> findReportsById(@Param("userId") int requestUserId);
}
and now the service's layer:
package com.ssc.test.cb3.service;
import com.ssc.test.cb3.repository.ReportRequestRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssc.test.cb3.model.ReportRequest;
/**
* Class to prepare the services to be dispatched upon request.
* @author ssc
*/
@Service
public class ReportRequestService {
@Autowired
private ReportRequestRepository reportRequestRepository;
/**
* Functionality to create a request containing a report of the chosen type of request
* @param reportRequest receives an objet ReportRequest with the information to
process the specific request
* @return the action of saving the parameter report in the database.
*/
public ReportRequest createReportRequest(ReportRequest reportRequest){
return reportRequestRepository.save(reportRequest);
}
}
Controller:
package com.ssc.test.cb3.controller;
// Quite los imports de aqui por que son muchos, pero todos estan
/**
* Class to handle REST services and APIs for the download Report's class
* @author ssc
*/
@RestController
@RequestMapping("/v1/reportRequest")
@CrossOrigin(origins = "http://localhost:3000")
public class ReportRequestController {
@Autowired
private ReportRequestService reportRequestService;
@PostMapping("/report")
private ReportRequest saveReportRequest(@RequestBody ReportRequest reportRequest){
return reportRequestService.createReportRequest(reportRequest);
}
}
I have tried to send the column nullable as true, but it doesn't work. If you have any idea on how I can process this petition I would appreciate you sharing your thoughts on it.
CodePudding user response:
I can see you are using entity class ReportRequest in the Controller for creating POST api but this is not a best practice. We should never expose entity classes directly from the controller. Best practice is to create DTO class on top of entity class and expose DTO class from the contoller. So I will suggest, first create DTO class.
Now as per your requirement you don't want to send requestDate attribute in JSON and you are thinking that if requestDate is not NULL or BLANK then hibernate automatically stores the current date in database table. That is incorrect. Hibernate won't store the date automatically. You need to write logic to store current date because the default value of every object in Java is NULL.
Note - In entity class ReportRequest please change data type of requestDate attribute from String to Date
You can write logic in setter method like this.
public void setRequestDate(Date requestDate){
if(requestDate==null){
this.requestDate=new Date();
}
}
I hope this solves your query.
CodePudding user response:
This is a partial update. There are some ways how to sew it up. This article explains it in a comprehensive manner: