Created
September 18, 2018 07:16
-
-
Save manuelleduc/f663523d5bc005c7b326b7ec7269c4a1 to your computer and use it in GitHub Desktop.
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
package test.testjpa.domain; | |
import javax.persistence.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Entity | |
public class A { | |
private Long id; | |
private String name; | |
private List<B> bs = new ArrayList<>(); | |
@Id | |
@GeneratedValue | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) | |
public List<B> getBs() { | |
return bs; | |
} | |
public void setBs(List<B> bs) { | |
this.bs = bs; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
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
package test.testjpa.domain; | |
import javax.persistence.*; | |
import java.util.List; | |
@Entity | |
public class B { | |
private Long id; | |
private List<A> as; | |
private long value; | |
public long getValue() { | |
return value; | |
} | |
public void setValue(long value) { | |
this.value = value; | |
} | |
@Id | |
@GeneratedValue | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
@ManyToMany(mappedBy = "bs") | |
public List<A> getAs() { | |
return as; | |
} | |
public void setAs(List<A> as) { | |
this.as = as; | |
} | |
} |
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
package jpa; | |
import test.testjpa.domain.A; | |
import test.testjpa.domain.B; | |
import test.testjpa.domain.Department; | |
import test.testjpa.domain.Employee; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityTransaction; | |
import javax.persistence.Persistence; | |
import javax.persistence.Query; | |
import java.util.List; | |
public class JpaTest { | |
private EntityManager manager; | |
public JpaTest(EntityManager manager) { | |
this.manager = manager; | |
} | |
public static void main(String[] args) { | |
final var factory = Persistence.createEntityManagerFactory("dev"); | |
final var manager = factory.createEntityManager(); | |
//final var test = new JpaTest(manager); | |
//final var tx = manager.getTransaction(); | |
//tx.begin(); | |
//try { | |
//test.createEmployees(); | |
//} catch (Exception e) { | |
// e.printStackTrace(); | |
//} | |
//tx.commit(); | |
//test.listEmployees(); | |
EntityTransaction tx = manager.getTransaction(); | |
tx.begin(); | |
for (int j = 0; j < 10; j++) { | |
final A a = new A(); | |
a.setName(String.valueOf(j)); | |
for (int i = 0; i < j; i++) { | |
final B b = new B(); | |
b.setValue(i); | |
manager.persist(b); | |
a.getBs().add(b); | |
} | |
manager.persist(a); | |
/*Department d = new Department("dep"+j); | |
manager.persist(d); | |
for (int i = 0; i < 2; i++) { | |
Employee e = new Employee("emp_"+j + "_"+i); | |
manager.persist(e); | |
e.setDepartment(d); | |
d.getEmployees().add(e); | |
}*/ | |
} | |
tx.commit(); | |
manager.close(); | |
System.out.println(".. done"); | |
} | |
private void listEmployees() { | |
var resultList = manager.createQuery("select a from Employee a", Employee.class).getResultList(); | |
System.out.println("Number of employees: " + resultList.size()); | |
for (var next : resultList) { | |
System.out.println("next employee: " + next); | |
} | |
} | |
private void createEmployees() { | |
final var numOfEmployees = manager.createQuery("select a from Employee a", Employee.class).getResultList().size(); | |
if (numOfEmployees == 0) { | |
final var department = new Department("java"); | |
manager.persist(department); | |
manager.persist(new Employee("Employee A", department)); | |
manager.persist(new Employee("Employee B", department)); | |
} | |
} | |
} |
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
package jpa; | |
import test.testjpa.domain.A; | |
import test.testjpa.domain.Department; | |
import test.testjpa.domain.Employee; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityTransaction; | |
import javax.persistence.Persistence; | |
import javax.persistence.Query; | |
import java.util.List; | |
public class JpaTestRM { | |
private EntityManager manager; | |
public JpaTestRM(EntityManager manager) { | |
this.manager = manager; | |
} | |
public static void main(String[] args) { | |
final var factory = Persistence.createEntityManagerFactory("dev"); | |
final var manager = factory.createEntityManager(); | |
//final var test = new JpaTest(manager); | |
//final var tx = manager.getTransaction(); | |
//tx.begin(); | |
//try { | |
//test.createEmployees(); | |
//} catch (Exception e) { | |
// e.printStackTrace(); | |
//} | |
//tx.commit(); | |
//test.listEmployees(); | |
EntityTransaction tx = manager.getTransaction(); | |
tx.begin(); | |
Query q = manager.createQuery("SELECT a FROM A a where a.id=120"); | |
A a = (A) q.getSingleResult(); | |
a.getBs().clear(); | |
manager.remove(a); | |
tx.commit(); | |
tx.begin(); | |
Query q2 = manager.createQuery("SELECT a FROM A a"); | |
List<A> as = q2.getResultList(); | |
for (A aa : as) { | |
System.out.println(aa + " = " + aa.getBs().size()); | |
} | |
tx.commit(); | |
manager.close(); | |
System.out.println(".. done"); | |
} | |
private void listEmployees() { | |
var resultList = manager.createQuery("select a from Employee a", Employee.class).getResultList(); | |
System.out.println("Number of employees: " + resultList.size()); | |
for (var next : resultList) { | |
System.out.println("next employee: " + next); | |
} | |
} | |
private void createEmployees() { | |
final var numOfEmployees = manager.createQuery("select a from Employee a", Employee.class).getResultList().size(); | |
if (numOfEmployees == 0) { | |
final var department = new Department("java"); | |
manager.persist(department); | |
manager.persist(new Employee("Employee A", department)); | |
manager.persist(new Employee("Employee B", department)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment