Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Cphdat3sem2017f/ORM-JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
Bladtman242 committed Aug 23, 2017
2 parents a88afd9 + 8a6da5c commit bc7b3c1
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DAT/jpa/src/main/java/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
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.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class User implements Serializable {

private static final long serialVersionUID = 1L;
Expand Down
69 changes: 69 additions & 0 deletions DAT/jpa/src/main/java/entity/UserBasic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class UserBasic extends User implements Serializable {

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

private String lastName;

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public Long getId()
{
return id;
}

public void setId(Long id)
{
this.id = id;
}

@Override
public int hashCode()
{
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object)
{
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof UserBasic))
{
return false;
}
UserBasic other = (UserBasic) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
{
return false;
}
return true;
}

@Override
public String toString()
{
return "entity.UserBasic[ id=" + id + " ]";
}

}
5 changes: 5 additions & 0 deletions DAT/jpa/src/main/java/jpacontrol/Populate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import entity.Address;
import entity.Shoe;
import entity.User;
import entity.UserBasic;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
Expand Down Expand Up @@ -46,6 +47,10 @@ public static void main(String[] args)
em.persist(s1);
em.persist(s2);

UserBasic ub = new UserBasic();
ub.setLastName("Johnson");
em.persist(ub);

em.getTransaction().commit();

em.close();
Expand Down
48 changes: 48 additions & 0 deletions DAT/jpa/src/main/java/jpacontrol/Populate2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package jpacontrol;

import entity.Address;
import entity.Shoe;
import entity.User;
import entity.UserBasic;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Populate2
{
public static void main(String[] args)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaPU");

EntityManager em = emf.createEntityManager();

em.getTransaction().begin();

User u = new User();
u.setFirstName("William");
em.persist(u);
u.setFirstName("George");
em.persist(u);

User u1 = new User();
u1.setFirstName("John");
u1.setId(222l);
em.merge(u1);
u1.setFirstName("Jack");
em.merge(u1);

User u2 = em.find(User.class, 222l);
System.out.println("FUNDET: " + u2.getFirstName());
u2.setFirstName("NoName");

em.merge(u2);

em.remove(u2);



em.getTransaction().commit();

em.close();
}
}
1 change: 1 addition & 0 deletions DAT/jpa/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<class>entity.Person</class>
<class>entity.Address</class>
<class>entity.Shoe</class>
<class>entity.UserBasic</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpadb"/>
<property name="javax.persistence.jdbc.user" value="root"/>
Expand Down

0 comments on commit bc7b3c1

Please sign in to comment.