shakeelosmani / springcoffeeshop

A simple Spring, Hibernate, Thymeleaf based CRUD application
7 stars 19 forks source link

Error in OrderController.java #3

Open AlpNek opened 4 years ago

AlpNek commented 4 years ago

Hi, Trying to learn some Java + Spring + stuff and your example is awesome.

However, I see an error when using the OrderController.java code. On line 60, it tells me: Cannot resolve method 'getProductPrice()

Due to this, not able to continue with build.

shakeelosmani commented 4 years ago

@AlpNek are you sure you have all the model code correctly available when you are building. The getProductPrice() is a simple getter in the Product class. Once the repository method returns an instance of a product by id we are simply calling the getter so it should work. Also you said OrderController I think that's a typo it is OrdersController.

AlpNek commented 4 years ago

Hi @shakeelosmani , thanks for replying promptly.

I found this comment on your blog and realised the error is because of newer version of springframework: https://shakeelosmani.wordpress.com/2017/02/13/step-by-step-spring-boot-hibernate-crud-web-application-tutorial/#comment-653

So changed the code to: for(Long productId:productIds) { if (!(productRepository.findById(productId).orElse(null).getProductPrice().isNaN())) { total = total + (productRepository.findById(productId).orElse(null).getProductPrice()); } to get it working. Not sure if this is the best way to do it though, since I am still learning :)

Cheers.

shakeelosmani commented 4 years ago

I see yeah that makes sense. Usually I think people would use the pom file and download dependencies that way it will work but downloading later versions may or may not work as I have not kept up with the updates in this example. And the updated code looks fine but it would be better to wrap it in a try catch as it could return null and then a possible exception

AlpNek commented 4 years ago

Thanks @shakeelosmani ! I'll test with try ... catch instead of checking for isNaN () which I'm sure will fall at some point.