Issue
friends. I have a some problem. I hope you can help me. I have merchant table, model, repository and service.
Merchant.java
@Data
@Entity
@Table(name = "Merchant")
public class Merchant {
@Id
private String term;
private String tag;
@Column(name = "tag_full")
private String tagFull;
}
MerchantRepository.java
public interface MerchantRepository extends JpaRepository<Merchant, String> {
}
MerchantService.java
@Service
@RequiredArgsConstructor
public class MerchantService {
private final MerchantRepository merchantRepository;
public Merchant getOne(String term) {
return merchantRepository.getOne(term);
}
}
The getOne function works correctly if I send a value that is in the table. But if I send a value that is not in the table, I get an error. How can i resolve this problem?
Solution
You Should Use findeOne() Instead getOne(). We use getOne() when The Entity is available. findOne() return Optional,Should Check If It Was Present , use get() to return Entity.
Answered By - Pirooz Azarabad