-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entity Managers: Create, Find, Update and Delete Student
- Loading branch information
1 parent
5ad0527
commit 7b74877
Showing
9 changed files
with
320 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,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> |
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,13 @@ | ||
package org.tutorial; | ||
|
||
/** | ||
* Hello world! | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
System.out.println( "Hello World!" ); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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()); | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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 + '\'' + | ||
'}'; | ||
} | ||
} |
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,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> |
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,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 ); | ||
} | ||
} |