Country country = countryService.findCountryByName(DEFAULT_COUNTRY_NAME)
.orElse(countryService.create(createCountryEntity()));
Служба:
public Optional<Country> findCountryByName(String name) {
return dao.findByNameIgnoreCase(name);
}
@Transactional(propagation = Propagation.REQUIRED)
public Country create(Country country) {
return dao.save(country);
}
Дао:
@Transactional
public interface CountryDao extends JpaRepository<Country, Integer> {
Optional<Country> findByNameIgnoreCase(String name);
}
Сущность
@Entity
@Table(name = "country")
@Data
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "country_id", updatable = false)
@JsonIgnore
private int id;
@Column(name = "country_name")
@NotNull
@Size(min = 4, max = 100)
private String name;
}
Я не знаю, почему countryService.findCountryByName(DEFAULT_COUNTRY_NAME)
.orElse(countryService.create(createCountryEntity()));
всегда идетв блок orElse
, даже если я убедился, что первая часть присутствует в отладчике.
Как это исправить?