2
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ What is the benefit of having Service Interface here? \$\endgroup\$
    – Pawan
    Feb 25 '19 at 7:06
  • \$\begingroup\$ @Pawan The use of service interface here is that if someone has to deliver ProductServices in a different way i.e by using a different database or using different business logic then by implementing the interface gives a reason to return the same output without affecting the presentation layer. \$\endgroup\$
    – New one
    Aug 30 '19 at 5:11
2
\$\begingroup\$

Well if I'd review this I'd add the following:

  • The Id field in Product could be renamed id to follow java convention
  • Generally it's a good practice to have equals & hashcode methods overridden.

As for the database connection it depends on which Spring & database you are using, does it happen to be Spring Boot?

\$\endgroup\$
1
  • \$\begingroup\$ @MaciejDymarczyk No problem Then it's very simple, assuming you use a mysql database you need: 1. Add dependency <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> ` </dependency>` 2. Add connection properties in application.yml file spring.datasource.url= jdbc:mysql://localhost:3306/YOUR_DB_NAME spring.datasource.username=YOUR_USERNAME spring.datasource.password=YOUR_PASSWORD For more check this \$\endgroup\$ May 21 '17 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.