Issue
I have 5 different object of class Tour with some fields.I want that my main page shows this object with their fields. I wrote conrtoller:
@Controller
public class MainController {
@Autowired
private TourRepository tourRepository;
@GetMapping("/")
public String mainPage(Model model) {
List<Tour> tourList=tourRepository.findAll();
model.addAttribute("tours",tourList);
return "home/main";
}
@RequestMapping("/main")
public String main() {
return "redirect:/";}
}
And Repository
public interface TourRepository extends JpaRepository<Tour,Integer> {
}
application.properties:
# hibernate configurations
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# thumeleaf configurations
spring.thymeleaf.mode= HTML
spring.thymeleaf.cache=false
Now I'm trying to pass variable values to the page with Thymeleaf,
<div>
<table>
<thead>
<tr>
<th>Title</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr th:each ="tour : ${tours}">
<td th:utext="${tour.title}">...</td>
<td th:utext="${tour.price}">...</td>
</tr>
</tbody>
</table>
</div>
but I get five identical duplicates of first record in db. How can I change controller\view?
Solution
My primary key named "code" in entity tout in db. Annotation @Id didn't a column with a different name as a primary key. I explicitly added:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "code")
private int id;
Answered By - Yan Zaitsau
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)