'Thymeleaf doesn't print list of objects (Spring JPA)
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 1:[1]
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andronicus |