Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Servlet-JPA/Hibernate task #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Table(name = "account")
public class Account {
@Id
@GeneratedValue
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name")
private String firstName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import java.util.List;


public class AccountDaoImpl implements AccountDao {

private final EntityManagerFactory emf;
Expand All @@ -21,31 +21,56 @@ public Account findOne(Long id) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();

//todo: find account by id using EntityManager
// todo: find account by id using EntityManager
Account account = em.find(Account.class, id);

em.getTransaction().commit();
em.close();

throw new UnsupportedOperationException("Method is not implemented yet. It's your homework!");
// todo: return account
return account;
}

@Override
public Account findByEmail(String email) {
public Account findByEmail(String email) throws NoResultException {
// todo: implement search by email via EntityManager
throw new UnsupportedOperationException("Method is not implemented yet. It's your homework!");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();

Account account = em.createQuery("Select a from Account a where a.email = :email", Account.class)
.setParameter("email", email)
.getSingleResult();

em.getTransaction().commit();
em.close();

return account;
}

@Override
public List<Account> findAll() {
//todo: find and return all accounts using EntityManagers
throw new UnsupportedOperationException("Method is not implemented yet. It's your homework!");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();

List<Account> accounts = em.createQuery("Select a from Account a")
.getResultList();

em.getTransaction().commit();
em.close();

return accounts;
}

@Override
public void save(Account account) {
// todo: save an account sing EntityManager
}
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();

em.persist(account);

em.getTransaction().commit();
em.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.bobocode.model.Account;

import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
Expand Down Expand Up @@ -45,8 +46,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
private void processGetRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
try {
Long id = fetchIdParameter(req);
String email = req.getParameter("email");
if (id != null) {
processGetById(req, resp, id);
} else if (email != null) {
processGetByEmail(req, resp, email);
} else {
processGetList(req, resp);
}
Expand All @@ -70,7 +74,7 @@ private Long fetchIdParameter(HttpServletRequest req) throws IOException {
try {
id = Long.parseLong(idStr);
} catch (NumberFormatException e) {
throw new InvalidRequestParametersException("Input parameter id in not valid");
throw new InvalidRequestParametersException("Input parameter id is not valid");
}
}
return id;
Expand Down Expand Up @@ -103,6 +107,18 @@ private void processGetById(HttpServletRequest req, HttpServletResponse resp, Lo
}
}

private void processGetByEmail(HttpServletRequest req, HttpServletResponse resp, String email) throws IOException, ServletException {
try {

Account account = accountDao.findByEmail(email);
req.setAttribute("account", account);
req.getRequestDispatcher("/account.jsp").forward(req, resp);

} catch (NoResultException e) {
handleNotFound(resp, "Account not found by email=" + email);
}
}

private void processGetList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Account> accountList = accountDao.findAll();
req.setAttribute("accountList", accountList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<class>com.bobocode.model.Account</class>

<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/bobocode_db"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="bobouser"/>
<property name="hibernate.connection.password" value="bobopass"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="admin"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="log4j.logger.org.hibernate" value="warn"/>

Expand Down