我正在使用spring-boot-neo4j中的示例,第37行的ProductServiceImpl类具有orElse()方法。
public Product getById(Long id) {
return productRepository.findById(id).orElse(null);
}
在我的情况下,我不知道为什么它被标记为“无法解析方法orElse(null),
为什么呢?我错过了什么?
这是我的产品域名
@NodeEntity
public class Product {
@GraphId
private Long id;
private String description;
private BigDecimal price;
private String imageUrl;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
//getters and setters
}
我的存储库
import guru.springframework.domain.Product;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.neo4j.repository.Neo4jRepository;
public interface ProductRepository extends GraphRepository<Product> {
Product findById(Long id);
Product deleteById(Long id);
}
服务
import guru.springframework.commands.ProductForm;
import guru.springframework.converters.ProductFormToProduct;
import guru.springframework.domain.Product;
import guru.springframework.repositories.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;
private ProductFormToProduct productFormToProduct;
@Autowired
public ProductServiceImpl(ProductRepository productRepository, ProductFormToProduct productFormToProduct) {
this.productRepository = productRepository;
this.productFormToProduct = productFormToProduct;
}
//some methods..
@Override
public Product getById(Long id) {
return productRepository.findById(id).orElse(null);
//return productRepository.findById(id);
}
}
pom文件与pom.xml
相同