Reputation: 1160
I have an Bid entity defined as follows
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@Entity
@Table(name = "bid_details")
public class Bid {
private enum STATUS { INITIATED, DRAFT, COMPLETED }
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Integer id;
@Column(name = "govt_bid_id", nullable = false)
private String govtBidNumber;
@Temporal(TemporalType.DATE)
@Column(name = "release_date", nullable = false)
@JsonFormat(pattern = "dd-MM-yyyy")
private Date releaseDate;
@ManyToOne(optional = false)
@JoinColumn(name = "created_by", referencedColumnName = "id", updatable = false, nullable = false)
private User createdBy;
@Temporal(TemporalType.DATE)
@Column(name = "created_date", nullable = false)
@CreationTimestamp
private Date createdDate;
@ManyToOne
@JoinColumn(name = "updated_by", referencedColumnName = "id", updatable = false, nullable = false)
private User updatedBy;
@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
private STATUS status;
@Column(name = "avg_turnover")
private String avgTurnover;
@Convert(converter = StringListConverter.class)
@Column(name = "docs_required", columnDefinition = "json")
private List<String> docsRequired;
@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = false)
private STATUS status;
}
and the corresponding columns are present in the bid_details
tables. I have bid repository defined as follows:
public interface BidRepository extends JpaRepository<Bid, Integer> {
}
now when I try to access data by id using findById
it is throwing No Value Present
exception whereas if I try to access the data using findAllById
I am getting correct result. Not able to figure out what's causing this weird behaviour.
Also, if I execute findAll
first and then findById
it is giving the correct result.
I am using spring-boot version 2.1.1
following is code where the entity is saved in the db
public Bid addBid(BidRequest bidRequest) {
User user = userRepository.findById(bidRequest.getCreatedBy()).get();
Bid bid = new Bid();
modelMapper.map(bidRequest, bid);
bid.setCreatedBy(user);
return bidRepository.save(bid);
}
BidRequest class is as follows:
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class BidRequest {
private String govtBidNumber;
@Temporal(TemporalType.DATE)
@JsonFormat(pattern = "yyyy-MM-dd")
private Date releaseDate;
@Temporal(TemporalType.DATE)
private Date endDate;
private int createdBy;
@Temporal(TemporalType.DATE)
@JsonFormat(pattern = "yyyy-MM-dd")
private Date createdDate;
private int updatedBy;
private String status;
private List<String> docsRequired;
}
Upvotes: 2
Views: 2787
Reputation: 1
I had the same issue. After turning on sql logs I found that Hibernate produced several inner joins for findById command.
Upvotes: 0
Reputation: 909
Have you tried orElse
like this
findById(id).orElse(null);
Because findById
returns an Optional
object so you have to write orElse()
after findById()
Upvotes: 3