Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.
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
37 changes: 37 additions & 0 deletions jpa-hibernate-servlet-task/src/main/java/com/bobocode/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.bobocode;

import com.bobocode.dao.AccountDao;
import com.bobocode.dao.impl.AccountDaoImpl;
import com.bobocode.model.Account;
import com.bobocode.util.AccountDataUtil;
import com.bobocode.util.DBUtil;

import java.util.List;

/**
* Created by hamster on 28.08.2017.
*/
public class Main {
public static void main(String[] args) {
AccountDao accountDao = new AccountDaoImpl(DBUtil.getEntityManagerFactory());
System.out.println("findOne");
System.out.println(accountDao.findOne(2L));
System.out.println("****************************************");

System.out.println("findByEmail");
System.out.println(accountDao.findByEmail("bob@marly.com"));
System.out.println("****************************************");

System.out.println("findAll");
List<Account> accountList = accountDao.findAll();
for (Account element:accountList) {
System.out.println(element);
}
System.out.println("****************************************");

System.out.println("save Account");
Account newAccount = AccountDataUtil.generateFakeAccount();
accountDao.save(newAccount);
System.out.println("****************************************");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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


Expand All @@ -22,29 +23,50 @@ public Account findOne(Long id) {
em.getTransaction().begin();

//todo: find account by id using EntityManager

Account queryAccount = em.createQuery("select a FROM Account a where a.id = :id", Account.class)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect. This functionality is already implemented in em.find()

.setParameter("id", id)
.getSingleResult();
em.getTransaction().commit();
em.close();

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

@Override
public Account findByEmail(String email) {
// 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 queryEmail = em.createQuery("select a from Account a where a.email = :email", Account.class)
.setParameter("email", email)
.getSingleResult();
em.getTransaction().commit();
em.close();
return queryEmail;
}

@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> queryAccountList = em.createQuery("select a from Account a", Account.class)
.getResultList();
em.getTransaction().commit();
em.close();
return queryAccountList;
}

@Override
public void save(Account account) {
// todo: save an account sing EntityManager
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(account);
System.out.println(account.toString() + " saccesfully saved!");
em.getTransaction().commit();
em.close();
}


Expand Down
2 changes: 1 addition & 1 deletion jpa-hibernate-servlet-task/src/main/webapp/form.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</div>
<div class="form-group">
<label for="birthday">Birthday</label>
<input type="date" name="birthday" class="form-control" id="birthday" placeholder="08/19/1969"/>
<input type="date" name="birthday" class="form-control" id="birthday" placeholder="1969-19-08"/>
</div>
<div class="form-group">
<label for="balance">Balance</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ private static Map getPropertiesMap() {

properties.put("hibernate.connection.url", "jdbc:postgresql://localhost:5432/bobocode_db");
properties.put("hibernate.connection.driver_class", "org.postgresql.Driver");
properties.put("hibernate.connection.username", "bobouser");
properties.put("hibernate.connection.password", "bobopass");
properties.put("hibernate.connection.username", "postgres");
properties.put("hibernate.connection.password", "qwerty");
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL9Dialect");
properties.put("hibernate.hbm2ddl.auto", "create");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/bobocode_db"/>
<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="qwerty"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="log4j.logger.org.hibernate" value="warn"/>

Expand Down