Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Doron Zehavi
Courses Plus Student 1,616 PointsItems not loading in ThymeLeaf for-each block using Hibernate
I am following along on the Hibernate with Spring course and I am writing a parallel project to the one in the course. I have duplicated the code identically to the course code and have the database working and accepting SQL statements in the browser.
My Spring application is also running and I am able to get to the correct page with the correct template.
However, my data is not loading in the container div. I have no idea how to further debug this as it is a web application. I have run out of ideas for what might have gone wrong. What should I do to determine and fix the problem?
The code for this portion of the template follows, and is using the name of the model for my parallel project.
<div class="news container">
<div class="row">
Newsitems -----
<div th:each="newsitem : ${newsfeed}" class="col s12 l4">
<div class="card">
<div class="card-content">
<div class="card-title">
<a href="http://google.com/" class="name" th:text="${newsitem.title}">Item Title</a>
</div>
</div>
</div>
</div>
</div>
</div>
Here is the code for the controller:
@Controller
public class FeedController {
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@RequestMapping(value = "/feed")
public String listNewsItems(Model model) {
Session session = sessionFactory.openSession();
List<NewsItem> newsFeedList = fetchAllNewsItems();
model.addAttribute("newsfeed", newsFeedList);
return "feed";
}
private List<NewsItem> fetchAllNewsItems() {
Session session = sessionFactory.openSession();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<NewsItem> criteria = builder.createQuery(NewsItem.class);
criteria.from(NewsItem.class);
List<NewsItem> newsItemList = session.createQuery(criteria).getResultList();
session.close();
return newsItemList;
}
}
Doron Zehavi
Courses Plus Student 1,616 PointsIt also looks like my database is not persisting rows that were added. I noticed it persisted it initially when I inserted the new row. But when I query now, it says there are no rows. Why is it not persisting?
This was the answer by the way. The database had no items in it. However, I previously added an item. When I found out the item was no longer persisted, I inserted it again and ran my application and the item appeared!
It looks like when I restart my spring server, it drops the table!
Doron Zehavi
Courses Plus Student 1,616 PointsDoron Zehavi
Courses Plus Student 1,616 PointsI was able to figure out how to debug the app in Intellij! (right click main method and click Debug Application.main())
It looks like my "FetchAllNewsItems" isn't returning a list.