вернуть ноль, используя FindById
Lead. java
import java.io.Serializable;
import java.time.LocalDate;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.lang.Nullable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
@Table(name = "leads")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},allowGetters = true)
public class Lead implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Name is mandatory")
private String name;
@Nullable
private String phone;
@Nullable
private String standard;
@Nullable
private String stream;
@Column(columnDefinition = "boolean default true")
private Boolean active = true;
@Nullable
private String remark;
@Column(columnDefinition = "VARCHAR(30) NOT NULL default 'lead'")
private String type="lead";
@ManyToOne(fetch = FetchType.LAZY, optional = false )//, cascade = CascadeType.REMOVE)
@JoinColumn(name = "school_id", nullable = false )
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
private School school;
@Nullable
@Column(name = "school_id", insertable = false, updatable = false)
private Long schoolId;
@ManyToOne
@JoinColumn(name = "lead_status_id", nullable = false)
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
private LeadStatus leadStatus;
@Nullable
@Column(name = "lead_status_id", insertable = false, updatable = false)
private Long leadStatusId;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
private User user;
@Nullable
@Column(name = "user_id", insertable = false, updatable = false)
private Long userId;
@CreationTimestamp
private LocalDate createdAt;
@UpdateTimestamp
private LocalDate updatedAt;
public Lead() {
// TODO Auto-generated constructor stub
}
public Lead(@NotBlank(message = "Name is mandatory") String name, String phone, String standard, String stream, String remark, String type, School school) {
this.name = name;
this.phone = phone;
this.standard = standard;
this.stream = stream;
this.remark = remark;
this.school = school;
}
public LeadStatus getLeadStatus() {
return leadStatus;
}
public void setLeadStatus(LeadStatus leadStatus) {
this.leadStatus = leadStatus;
}
public Long getLeadStatusId() {
return leadStatusId;
}
public void setLeadStatusId(Long leadStatusId) {
this.leadStatusId = leadStatusId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public String getStream() {
return stream;
}
public void setStream(String stream) {
this.stream = stream;
}
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public Long getSchoolId() {
return schoolId;
}
public void setSchoolId(Long schoolId) {
this.schoolId = schoolId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public LocalDate getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDate createdAt) {
this.createdAt = createdAt;
}
public LocalDate getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(LocalDate updatedAt) {
this.updatedAt = updatedAt;
}
}
Метод контроллера
@GetMapping("/{id}")
public HashMap<String, Object> getLeadById(@PathVariable(value = "id") Long leadId) {
System.out.println(leadId);
Lead lead = leadRepository.findById(leadId).orElseThrow(() -> new ItemNotFoundException("Lead", "id", leadId));
return ResponseFormat.createFormat(lead, "Listed Successfully");
}
LeadRepository. java
package com.lakshya.lakshya.repository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.lakshya.lakshya.model.Lead;
public interface LeadRepository extends PagingAndSortingRepository<Lead, Long> {
}