• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doing a select following this MVC approach in a simple Web App

 
Fernando Poliero
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm developing a very simple web application which interacts with a little database. I'm using Netbeans and PostgreSQL.

The goal is to use some kind of simplified Model View Controller pattern, specifically using JSP as view, and Java classes as controller and model.

So far, I have developed the main menu of my web app, index.html



The index has a link to the next page createUser.html, a form in which info about a new user must be filled.



This one calls the servlet addUser.java



The servlet uses the entity class User.java which does the SQL operation on the DB.



So far, so good but unfortunately the tutorial I was following ends abruptely here and I'm just beginning with all these things, so I'm lost for the next part!

What I'd want to do now is implement 2 more operations in the main menu, one for showing all the data in the users table and one for deleting a record in the users table.

Of course using the same approach I've shown here for the insert (when creating a new user).

Please note I've already made by myself a working solution with a JSP containing also the SQL code to access the DB. Then the same JSP also shows the result obtained in the page. But of course this is not the good way to go because the JSP should not do the business logic nor the DB operations.

So, can anyone show me at least the code for one of the two operations I've mentioned above, let's say the "View all users" operation, which does a "Select * from users" and show the results?

Please remember! It must do so following the MVC approach used for the operation "Create new User" whose code (servlet class, entity class and JSP) has been shown up above.

Thanks.
 
Stefan Evans
Bartender
Posts: 1807
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JSP perspective, I would expect to get the data via a request attribute being a List of User objects
The JSP would then use a JSTL c:forEach tag to iterate over the list and display each users details.


The servlet obviously be responsible for retrieving the list of users, and putting it into a request attribute for the JSP.

Which leaves the "how do you query your data" question.

And the answer probably shouldn't "select * from users"
You've already abstracted away the saving of your user via a Hibernate session.

So it would make more sense to use a Hibernate session to perform your other database operations.
What else can you do in a hibernate session rather than "save" items?
You might look at doing a Hibernate query rather than a SQL one.
 
Fernando Poliero
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:

And the answer probably shouldn't "select * from users"
You've already abstracted away the saving of your user via a Hibernate session.

So it would make more sense to use a Hibernate session to perform your other database operations.
What else can you do in a hibernate session rather than "save" items?
You might look at doing a Hibernate query rather than a SQL one.


Thing is I'm new at Hibernate... I didn't understand how the saving operation works without the query... there is not "insert" in the code of my previous post as you can see.

I need also to do some query involving a JOIN among two or more tables, how can be achieved with Hibernate which does not contain any SQL code?

Can you show me at least the code for retrieving the info (the same info a "select * from users" query would provide) and how it's done with Hibernate?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using the JPA bindings to Hibernate?
 
Stefan Evans
Bartender
Posts: 1807
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the annotations in the User class above I think the answer to that question is: Yes, JPA bindings are being used.
This is getting a little off topic for the Servlets forum, and I am by no means a Hibernate expert but here goes.

Basically, hibernate abstracts away your database so you don't need to write insert/select statements against your database.
You've already managed to create a User object and tie it to the "users" table via your JPA bindings.
You have opened a session, and saved it via the save() method (which would have executed "insert into users...").


To retrieve a list of all your users (select * from user) you could do something like:


 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to add my two cents here. I think that when someone is just starting to learn JEE and the MVC model, they should NOT be using frameworks like Hibernate and Spring. It too early in the learning curve for you to be trying to learn Hibernate. Learn how to write a DAO that uses PreparedStatements and gets a connection from the pool. You need to understand things like connection pooling and InitialContext before you start relying on frameworks.

Later on when you start using things like Hibernate you'll be able to understand what it's doing for you. At this point it's just a mysterious black box that is creating more complexity that you need.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic