-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cnls
committed
Aug 24, 2017
1 parent
db5dba0
commit 0b88f60
Showing
17 changed files
with
374 additions
and
66 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package facade; | ||
|
||
import entity.User; | ||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
public class FacadeUser implements FacadeUserInterface | ||
{ | ||
private EntityManagerFactory emf; | ||
|
||
public FacadeUser(EntityManagerFactory emf) | ||
{ | ||
this.emf = emf; | ||
} | ||
|
||
@Override | ||
public User getUser(Long id) | ||
{ | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
try | ||
{ | ||
em.getTransaction().begin(); | ||
User user = em.find(User.class, id); | ||
em.getTransaction().commit(); | ||
return user; | ||
} | ||
finally | ||
{ | ||
em.close(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public User addUser(User u) | ||
{ | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
try | ||
{ | ||
em.getTransaction().begin(); | ||
em.persist(u); | ||
em.getTransaction().commit(); | ||
return u; | ||
} | ||
finally | ||
{ | ||
em.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public User deleteUser(Long id) | ||
{ | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
try | ||
{ | ||
em.getTransaction().begin(); | ||
User user = em.find(User.class, id); | ||
if( user != null) | ||
{ | ||
em.remove(user); | ||
} | ||
em.getTransaction().commit(); | ||
return user; | ||
} | ||
finally | ||
{ | ||
em.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public User editUser(User u) | ||
{ | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
try | ||
{ | ||
em.getTransaction().begin(); | ||
User user = em.find(User.class, u.getId()); | ||
if( user != null) | ||
{ | ||
em.merge(u); | ||
} | ||
em.getTransaction().commit(); | ||
return user; | ||
} | ||
finally | ||
{ | ||
em.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public List<User> getUsers() | ||
{ | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
List<User> users = null; | ||
|
||
try | ||
{ | ||
em.getTransaction().begin(); | ||
users = em.createQuery("Select u from User u").getResultList(); | ||
em.getTransaction().commit(); | ||
return users; | ||
} | ||
finally | ||
{ | ||
em.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package facade; | ||
|
||
import entity.User; | ||
import java.util.List; | ||
|
||
public interface FacadeUserInterface | ||
{ | ||
public User getUser(Long id); | ||
public User addUser(User u); | ||
public User deleteUser(Long id); | ||
public User editUser(User u); | ||
public List<User> getUsers(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jpacontrol; | ||
|
||
import entity.User; | ||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
import javax.persistence.Query; | ||
|
||
public class JPQL | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "jpaPU" ); | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
em.getTransaction().begin(); | ||
|
||
//NamedQuery | ||
Query Nquery = em.createNamedQuery("UsersFindBorn"); | ||
Nquery.setParameter("userBorn", true); | ||
List<User> usersBorn = Nquery.getResultList(); | ||
for(User u:usersBorn) | ||
{ | ||
System.out.println("User: " + u); | ||
} | ||
|
||
//DynamicQuery1 | ||
Query Dquery1 = em.createQuery("SELECT u FROM User u"); | ||
List<User> usersAll = Dquery1.getResultList(); | ||
for(User u:usersAll) | ||
{ | ||
System.out.println("User: " + u); | ||
} | ||
|
||
//DynamicQuery2 | ||
Query DQuery2 = em.createQuery("Select UPPER(u.firstName) from User u"); | ||
List<String> firstnames = DQuery2.getResultList(); | ||
for(String firstname:firstnames) | ||
{ | ||
System.out.println("User: " + firstname); | ||
} | ||
|
||
//DynamicQuery3 | ||
Query DQuery3 = em.createQuery("Select MAX(u.id) from User u"); | ||
Long maximum = (Long) DQuery3.getSingleResult(); | ||
System.out.println("Max UserID :" + maximum); | ||
|
||
em.getTransaction().commit(); | ||
em.close(); | ||
} | ||
} |
Oops, something went wrong.