Skip to content

Commit

Permalink
Entity Managers: Create, Find, Update and Delete Student
Browse files Browse the repository at this point in the history
  • Loading branch information
gscaramuzzino committed Jul 20, 2020
1 parent 5ad0527 commit 7b74877
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 0 deletions.
89 changes: 89 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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>org.tutorial</groupId>
<artifactId>jpa</artifactId>
<version>1.0-SNAPSHOT</version>

<name>jpa</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.18.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>

</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
13 changes: 13 additions & 0 deletions src/main/java/org/tutorial/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.tutorial;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/tutorial/CreateStudent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.tutorial;

import org.tutorial.model.Student;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class CreateStudent {
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("student_pu");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

entityTransaction.begin();

Student student = new Student();
student.setFirstName("John");
student.setLastName("Bell");

Student student1 = new Student();
student1.setFirstName("Alan");
student1.setLastName("Conrad");

entityManager.persist(student);

entityTransaction.commit();

entityManager.close();
entityManagerFactory.close();
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/tutorial/DeleteStudent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.tutorial;

import org.tutorial.model.Student;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class DeleteStudent {

public static void main(String[] args) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("student_pu");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

Student student = entityManager.find(Student.class, 1L);

System.out.println(student.toString());

entityTransaction.begin();

entityManager.remove(student);

entityTransaction.commit();

entityManager.close();
entityManagerFactory.close();
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/tutorial/FindStudent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.tutorial;

import org.tutorial.model.Student;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class FindStudent {

public static void main(String[] args) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("student_pu");
EntityManager entityManager = entityManagerFactory.createEntityManager();

Student student = entityManager.find(Student.class, 1L);

if (student != null) {
System.out.println(student.toString());
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/tutorial/UpdateStudent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.tutorial;

import org.tutorial.model.Student;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class UpdateStudent {

public static void main(String[] args) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("student_pu");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();

Student student = entityManager.find(Student.class, 1L);
System.out.println("Before update " + student.toString());

entityTransaction.begin();

student.setFirstName("Alan");
student.setLastName("Bold");

entityTransaction.commit();

System.out.println("After update " + student.toString());

entityManager.close();
entityManagerFactory.close();
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/tutorial/model/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.tutorial.model;

import javax.persistence.*;

@Entity
@Table(name = "student")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "first_name", nullable = false, length = 150)
private String firstName;

@Column(name = "last_name", nullable = false, length = 250)
private String lastName;

public Student() {
}

public Student(Long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}

public Long getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

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

@Override
public String toString() {
return "Student{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
19 changes: 19 additions & 0 deletions src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="student_pu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

<class>org.tutorial.model.Student</class>

<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:~/test"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="create"/> -->
</properties>
</persistence-unit>
</persistence>
20 changes: 20 additions & 0 deletions src/test/java/org/tutorial/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.tutorial;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}

0 comments on commit 7b74877

Please sign in to comment.