Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
cnls committed Aug 24, 2017
1 parent db5dba0 commit 0b88f60
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 66 deletions.
Binary file modified DAT/ORM_JPA_JPQL-Notes.pdf
Binary file not shown.
26 changes: 22 additions & 4 deletions DAT/jpa/src/main/java/entity/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Address implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String street;
Expand All @@ -27,8 +27,6 @@ public Address(String street, String city)
this.city = city;
}



public Long getId()
{
return id;
Expand All @@ -39,6 +37,26 @@ public void setId(Long id)
this.id = id;
}

public String getStreet()
{
return street;
}

public void setStreet(String street)
{
this.street = street;
}

public String getCity()
{
return city;
}

public void setCity(String city)
{
this.city = city;
}

@Override
public int hashCode()
{
Expand Down Expand Up @@ -69,4 +87,4 @@ public String toString()
return "entity.Address[ id=" + id + " ]";
}

}
}
6 changes: 3 additions & 3 deletions DAT/jpa/src/main/java/entity/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public Person()
{
}

public Person(Integer idPerson)
public Person(String mail)
{
this.idPerson = idPerson;
this.mail = mail;
}

public Integer getIdPerson()
Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean equals(Object object)
@Override
public String toString()
{
return "entity.Person[ idPerson=" + idPerson + " ]";
return "entity.Person[ idPerson=" + idPerson + ", mail=" + mail + " ]";
}

}
23 changes: 22 additions & 1 deletion DAT/jpa/src/main/java/entity/Shoe.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,34 @@
@Entity
public class Shoe implements Serializable {

@ManyToMany(mappedBy = "shoes")
@ManyToMany(mappedBy="shoes")
private List<User> users = new ArrayList();

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String title;

public Shoe()
{
}

public Shoe(String title)
{
this.title = title;
}

public String getTitle()
{
return title;
}

public void setTitle(String title)
{
this.title = title;
}

public Long getId()
{
Expand Down
36 changes: 25 additions & 11 deletions DAT/jpa/src/main/java/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@NamedQueries(
{
@NamedQuery(name = "UsersFindBorn", query = "SELECT u FROM User u WHERE u.born = :userBorn")
})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class User implements Serializable {

private static final long serialVersionUID = 1L;
Expand All @@ -28,21 +35,32 @@ public class User implements Serializable {

private String firstName;

private Date testDate;

@Temporal(TemporalType.DATE)
private Date creationDate;

@Temporal(TemporalType.TIME)
private Date testDate;
@Temporal(TemporalType.TIMESTAMP)
private Date expirationDate;

@Transient
private boolean born;

@Transient
private String temp;

private Address address;

@ManyToMany
@ManyToMany//(cascade=CascadeType.PERSIST)
private List<Shoe> shoes = new ArrayList<>();

public User()
{
}

public User(String firstName, boolean born)
{
this.firstName = firstName;
this.born = born;
}

public Address getAddress()
{
Expand All @@ -69,8 +87,6 @@ public void setShoes(List<Shoe> shoes)
this.shoes = shoes;
}



public Long getId()
{
return id;
Expand All @@ -91,8 +107,6 @@ public void setFirstName(String firstName)
this.firstName = firstName;
}



@Override
public int hashCode()
{
Expand Down Expand Up @@ -120,7 +134,7 @@ public boolean equals(Object object)
@Override
public String toString()
{
return "entity.User[ id=" + id + " ]";
return "entity.User[ id=" + id + ", firstName=" + firstName + ", born=" + born + " ]";
}

}
117 changes: 117 additions & 0 deletions DAT/jpa/src/main/java/facade/FacadeUser.java
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();
}
}
}
13 changes: 13 additions & 0 deletions DAT/jpa/src/main/java/facade/FacadeUserInterface.java
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();
}
52 changes: 52 additions & 0 deletions DAT/jpa/src/main/java/jpacontrol/JPQL.java
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();
}
}
Loading

0 comments on commit 0b88f60

Please sign in to comment.