diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/User.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/defaultvalues/User.java
similarity index 93%
rename from persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/User.java
rename to persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/defaultvalues/User.java
index e271c01815f2..436c708d4060 100644
--- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/User.java
+++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/defaultvalues/User.java
@@ -1,4 +1,4 @@
-package com.baeldung.jpadefaultvalues.application;
+package com.baeldung.jpa.defaultvalues;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -9,7 +9,6 @@
public class User {
@Id
- @GeneratedValue
private Long id;
@Column(columnDefinition = "varchar(255) default 'John Snow'")
diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/defaultvalues/UserRepository.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/defaultvalues/UserRepository.java
new file mode 100644
index 000000000000..52f9807cfbea
--- /dev/null
+++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/defaultvalues/UserRepository.java
@@ -0,0 +1,36 @@
+package com.baeldung.jpa.defaultvalues;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+
+public class UserRepository {
+
+ private EntityManagerFactory emf = null;
+
+ public UserRepository() {
+ emf = Persistence.createEntityManagerFactory("entity-default-values");
+ }
+
+ public User find(Long id) {
+ EntityManager entityManager = emf.createEntityManager();
+ User user = entityManager.find(User.class, id);
+ entityManager.close();
+ return user;
+ }
+
+ public void save(User user, Long id) {
+ user.setId(id);
+
+ EntityManager entityManager = emf.createEntityManager();
+ entityManager.getTransaction().begin();
+ entityManager.persist(user);
+ entityManager.getTransaction().commit();
+ entityManager.close();
+ }
+
+ public void clean() {
+ emf.close();
+ }
+
+}
diff --git a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml
index 5422afa4a3c5..21a757f49053 100644
--- a/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml
+++ b/persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml
@@ -1,133 +1,149 @@
-
-
-
-
- org.hibernate.jpa.HibernatePersistenceProvider
- com.baeldung.sqlresultsetmapping.ScheduledDay
- com.baeldung.sqlresultsetmapping.Employee
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
- org.hibernate.jpa.HibernatePersistenceProvider
- com.baeldung.jpa.stringcast.Message
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
- org.hibernate.jpa.HibernatePersistenceProvider
- com.baeldung.jpa.model.Car
- true
-
-
-
-
-
-
-
-
-
-
-
- com.baeldung.jpa.entitygraph.model.Post
- com.baeldung.jpa.entitygraph.model.User
- com.baeldung.jpa.entitygraph.model.Comment
- true
-
-
-
-
-
-
-
-
-
-
-
-
- org.eclipse.persistence.jpa.PersistenceProvider
- com.baeldung.jpa.datetime.JPA22DateTimeEntity
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- org.hibernate.jpa.HibernatePersistenceProvider
- com.baeldung.jpa.criteria.entity.Item
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- org.hibernate.jpa.HibernatePersistenceProvider
- com.baeldung.jpa.querytypes.UserEntity
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
+
+
+
+
+ org.hibernate.jpa.HibernatePersistenceProvider
+ com.baeldung.sqlresultsetmapping.ScheduledDay
+ com.baeldung.sqlresultsetmapping.Employee
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.hibernate.jpa.HibernatePersistenceProvider
+ com.baeldung.jpa.stringcast.Message
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.hibernate.jpa.HibernatePersistenceProvider
+ com.baeldung.jpa.model.Car
+ true
+
+
+
+
+
+
+
+
+
+
+
+ com.baeldung.jpa.entitygraph.model.Post
+ com.baeldung.jpa.entitygraph.model.User
+ com.baeldung.jpa.entitygraph.model.Comment
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+ org.eclipse.persistence.jpa.PersistenceProvider
+ com.baeldung.jpa.datetime.JPA22DateTimeEntity
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.hibernate.jpa.HibernatePersistenceProvider
+ com.baeldung.jpa.criteria.entity.Item
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.hibernate.jpa.HibernatePersistenceProvider
+ com.baeldung.jpa.querytypes.UserEntity
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.hibernate.jpa.HibernatePersistenceProvider
+ com.baeldung.jpa.defaultvalues.User
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/jpadefaultvalues/application/UserDefaultValuesUnitTest.java b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/defautlvalues/UserDefaultValuesUnitTest.java
similarity index 59%
rename from persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/jpadefaultvalues/application/UserDefaultValuesUnitTest.java
rename to persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/defautlvalues/UserDefaultValuesUnitTest.java
index 52a5c4dcd6c5..dc41ff51f29b 100644
--- a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/jpadefaultvalues/application/UserDefaultValuesUnitTest.java
+++ b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/defautlvalues/UserDefaultValuesUnitTest.java
@@ -1,31 +1,31 @@
-package com.baeldung.jpadefaultvalues.application;
+package com.baeldung.jpa.defaultvalues;
-import com.baeldung.jpadefaultvalues.application.User;
-import com.baeldung.jpadefaultvalues.application.UserRepository;
+import com.baeldung.jpa.defaultvalues.User;
+import com.baeldung.jpa.defaultvalues.UserRepository;
import org.junit.Test;
import org.junit.Ignore;
-import org.junit.runner.RunWith;
-
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.test.context.junit4.SpringRunner;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import static org.junit.Assert.*;
-@RunWith(SpringRunner.class)
-@SpringBootTest
public class UserDefaultValuesUnitTest {
- @Autowired
- private UserRepository userRepository;
+ private static UserRepository userRepository = null;
+
+ @BeforeClass
+ public static void once() {
+ userRepository = new UserRepository();
+ }
@Test
@Ignore // SQL default values are also defined
public void saveUser_shouldSaveWithDefaultFieldValues() {
User user = new User();
- user = userRepository.save(user);
+ userRepository.save(user, 1L);
+ user = userRepository.find(1L);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
@@ -36,8 +36,9 @@ public void saveUser_shouldSaveWithDefaultFieldValues() {
public void saveUser_shouldSaveWithNullName() {
User user = new User();
user.setName(null);
- user = userRepository.save(user);
+ userRepository.save(user, 2L);
+ user = userRepository.find(2L);
assertNull(user.getName());
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
@@ -46,11 +47,17 @@ public void saveUser_shouldSaveWithNullName() {
@Test
public void saveUser_shouldSaveWithDefaultSqlValues() {
User user = new User();
- user = userRepository.save(user);
+ userRepository.save(user, 3L);
+ user = userRepository.find(3L);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
+ @AfterClass
+ public static void destroy() {
+ userRepository.clean();
+ }
+
}
diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/Application.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/Application.java
deleted file mode 100644
index 3b96f685837a..000000000000
--- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/Application.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.baeldung.jpadefaultvalues.application;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.ApplicationContext;
-
-@SpringBootApplication
-public class Application {
- private static ApplicationContext applicationContext;
-
- public static void main(String[] args) {
- applicationContext = SpringApplication.run(Application.class, args);
- }
-}
diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/UserRepository.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/UserRepository.java
deleted file mode 100644
index ca0e4c4d7181..000000000000
--- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/jpadefaultvalues/application/UserRepository.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package com.baeldung.jpadefaultvalues.application;
-
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public interface UserRepository extends JpaRepository {
-}