I'm building a small application in Spring and Spring Data JPA. I need serval layer. I don't know if I'm doing well.
Here is an example:
POJO
@Entity
public class Product {
@Id
@GeneratedValue
private long Id;
@NotBlank
private String name;
@NotNull
private int price;
public long getId() {
return Id;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public void setId(long id) {
Id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"Id=" + Id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
REPOSITORY
public interface ProductRepository extends CrudRepository<Product, Long> {
Product findByName(String name);
List<Product> getAllProducts();
Product getProductById(String productId);
void addProduct(Product product);
}
SERVICE
public interface ProductService {
List<Product> getAllProducts();
Product getProductById(String productId);
void addProduct(Product product);
}
REPOSITORY IMPLEMENTATION
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductRepository productRepository;
@Override
public List<Product> getAllProducts() {
return productRepository.getAllProducts();
}
@Override
public Product getProductById(String productId) {
return productRepository.getProductById(productId);
}
@Override
public void addProduct(Product product) {
productRepository.addProduct(product);
}
}
It is a good? Unless How create communication with database in Spring?