-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit e5e2ed9
Showing
10 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
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,10 @@ | ||
---------------------------------------------------------------- | ||
Thu Nov 15 13:30:22 EET 2018: | ||
Booting Derby version The Apache Software Foundation - Apache Derby - 10.8.2.2 - (1181258): instance a816c00e-0167-1722-e50d-000006f22160 | ||
on database directory /mnt/c/Users/Pascal Fares/Documents/NetBeansProjects/testJPAeclipselink/simpleDb with class loader sun.misc.Launcher$AppClassLoader@7f31245a | ||
Loaded from file:/mnt/c/Users/Pascal%20Fares/Documents/NetBeansProjects/testJPAeclipselink/target/alternateLocation/derby-10.8.2.2.jar | ||
java.vendor=Oracle Corporation | ||
java.runtime.version=1.8.0_181-8u181-b13-1ubuntu0.18.04.1-b13 | ||
user.dir=/mnt/c/Users/Pascal Fares/Documents/NetBeansProjects/testJPAeclipselink | ||
derby.system.home=null | ||
Database Class Loader started - derby.database.classpath='' |
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,3 @@ | ||
ij.protocol=jdbc:derby: | ||
ij.database=simpleDb | ||
ij.maximumDisplayWidth=20 |
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,3 @@ | ||
mvn package | ||
cd target | ||
java -cp 'lib/*:testJPAeclipselink-1.0-SNAPSHOT.jar' lb.edu.isae.testjpaeclipselink.jpa.JpaTest |
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,72 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>lb.edu.isae</groupId> | ||
<artifactId>testJPAeclipselink</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<repositories> | ||
<repository> | ||
<id>EclipseLink Repo</id> | ||
<url>http://download.eclipse.org/rt/eclipselink/maven.repo</url> | ||
<name>EclipseLink Repo</name> | ||
</repository> | ||
</repositories> | ||
<dependencies> | ||
|
||
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.13</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.eclipse.persistence</groupId> | ||
<artifactId>eclipselink</artifactId> | ||
<version>2.2.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.persistence</groupId> | ||
<artifactId>javax.persistence</artifactId> | ||
<version>2.0.3</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<version>3.1.1</version> | ||
<executions> | ||
<execution> | ||
<id>copy-dependencies</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>copy-dependencies</goal> | ||
</goals> | ||
<configuration> | ||
<outputDirectory>${project.build.directory}/lib</outputDirectory> | ||
<overWriteReleases>false</overWriteReleases> | ||
<overWriteSnapshots>false</overWriteSnapshots> | ||
<overWriteIfNewer>true</overWriteIfNewer> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<addClasspath>true</addClasspath> | ||
<classpathPrefix>lib/</classpathPrefix> | ||
<mainClass>theMainClass</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
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 @@ | ||
mvn compile exec:java -Dexec.mainClass=lb.edu.isae.testjpaeclipselink.jpa.JpaTest |
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,2 @@ | ||
mvn dependency:copy-dependencies | ||
java -cp 'target/dependency/*' org.apache.derby.tools.ij -p ij.properties |
51 changes: 51 additions & 0 deletions
51
src/main/java/lb/edu/isae/testjpaeclipselink/domain/Department.java
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,51 @@ | ||
package lb.edu.isae.testjpaeclipselink.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.OneToMany; | ||
|
||
@Entity | ||
public class Department { | ||
|
||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
|
||
private String name; | ||
|
||
@OneToMany(mappedBy="department",cascade=CascadeType.PERSIST) | ||
private List<Employee> employees = new ArrayList<Employee>(); | ||
|
||
|
||
|
||
public Department() { | ||
super(); | ||
} | ||
public Department(String name) { | ||
this.name = name; | ||
} | ||
public Long getId() { | ||
return id; | ||
} | ||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public List<Employee> getEmployees() { | ||
return employees; | ||
} | ||
public void setEmployees(List<Employee> employees) { | ||
this.employees = employees; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/lb/edu/isae/testjpaeclipselink/domain/Employee.java
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,61 @@ | ||
package lb.edu.isae.testjpaeclipselink.domain; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToOne; | ||
|
||
@Entity | ||
public class Employee { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@ManyToOne | ||
private Department department; | ||
|
||
public Employee() {} | ||
|
||
public Employee(String name, Department department) { | ||
this.name = name; | ||
this.department = department; | ||
} | ||
|
||
|
||
public Employee(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Department getDepartment() { | ||
return department; | ||
} | ||
|
||
public void setDepartment(Department department) { | ||
this.department = department; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Employee [id=" + id + ", name=" + name + ", department=" | ||
+ department.getName() + "]"; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/lb/edu/isae/testjpaeclipselink/jpa/JpaTest.java
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,65 @@ | ||
package lb.edu.isae.testjpaeclipselink.jpa; | ||
|
||
import java.util.List; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.EntityTransaction; | ||
import javax.persistence.Persistence; | ||
|
||
import lb.edu.isae.testjpaeclipselink.domain.Employee; | ||
import lb.edu.isae.testjpaeclipselink.domain.Department; | ||
|
||
public class JpaTest { | ||
|
||
private final EntityManager manager; | ||
|
||
public JpaTest(EntityManager manager) { | ||
this.manager = manager; | ||
} | ||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit"); | ||
EntityManager manager = factory.createEntityManager(); | ||
JpaTest test = new JpaTest(manager); | ||
|
||
EntityTransaction tx = manager.getTransaction(); | ||
tx.begin(); | ||
|
||
test.createEmployees(); | ||
|
||
tx.commit(); | ||
|
||
test.listEmployees(); | ||
|
||
System.out.println(".. done"); | ||
} | ||
|
||
|
||
|
||
|
||
private void createEmployees() { | ||
int numOfEmployees = manager.createQuery("Select a From Employee a", Employee.class).getResultList().size(); | ||
if (numOfEmployees == 0) { | ||
Department department = new Department("java cycle C"); | ||
manager.persist(department); | ||
|
||
manager.persist(new Employee("Pascal E Fares",department)); | ||
manager.persist(new Employee("Etudiant Cnam",department)); | ||
|
||
} | ||
} | ||
|
||
|
||
private void listEmployees() { | ||
List<Employee> resultList = manager.createQuery("Select a From Employee a", Employee.class).getResultList(); | ||
System.out.println("num of employess:" + resultList.size()); | ||
for (Employee next : resultList) { | ||
System.out.println("next employee: " + next); | ||
} | ||
} | ||
|
||
|
||
} |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> | ||
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"> | ||
<!-- shouldn't be valid for java SE per specification, but it works for EclipseLink ... --> | ||
<exclude-unlisted-classes>false</exclude-unlisted-classes> | ||
<properties> | ||
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> | ||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testDB"/> | ||
<property name="javax.persistence.jdbc.user" value="pascalfares"/> | ||
<property name="javax.persistence.jdbc.password" value="pascal00"/> | ||
<!-- EclipseLink should create the database schema automatically --> | ||
<property name="eclipselink.ddl-generation" value="drop-create-tables"/> | ||
<property name="eclipselink.ddl-generation.output-mode" value="database"/> | ||
<property name="eclipselink.logging.level" value="SEVERE"/> | ||
</properties> | ||
</persistence-unit> | ||
</persistence> |