diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Admin.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Admin.java new file mode 100644 index 0000000000..1c59b33ab8 --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Admin.java @@ -0,0 +1,36 @@ +package com.baeldung.jpa.generateidvalue; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "app_admin") +public class Admin { + + @Id + @GeneratedValue + private Long id; + + @Column(name = "admin_name") + private String 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; + } + +} \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Article.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Article.java new file mode 100644 index 0000000000..06465de179 --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Article.java @@ -0,0 +1,39 @@ +package com.baeldung.jpa.generateidvalue; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + +@Entity +@Table(name = "article") +public class Article { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "article_gen") + @SequenceGenerator(name = "article_gen", sequenceName = "article_seq") + private Long id; + + @Column(name = "article_name") + private String 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; + } + +} \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/IdGenerator.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/IdGenerator.java new file mode 100644 index 0000000000..0fac86747f --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/IdGenerator.java @@ -0,0 +1,48 @@ +package com.baeldung.jpa.generateidvalue; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Table(name = "id_gen") +@Entity +public class IdGenerator { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "gen_name") + private String gen_name; + + @Column(name = "gen_value") + private Long gen_value; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getGen_name() { + return gen_name; + } + + public void setGen_name(String gen_name) { + this.gen_name = gen_name; + } + + public Long getGen_value() { + return gen_value; + } + + public void setGen_value(Long gen_value) { + this.gen_value = gen_value; + } + +} \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Task.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Task.java new file mode 100644 index 0000000000..a51ec53418 --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Task.java @@ -0,0 +1,39 @@ +package com.baeldung.jpa.generateidvalue; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.TableGenerator; + +@Entity +@Table(name = "task") +public class Task { + + @Id + @TableGenerator(name = "id_generator", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_value", pkColumnValue = "task_gen", initialValue = 10000, allocationSize = 10) + @GeneratedValue(strategy = GenerationType.TABLE, generator = "id_generator") + private Long id; + + @Column(name = "name") + private String 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; + } + +} \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/User.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/User.java new file mode 100644 index 0000000000..88fefe7ba6 --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/User.java @@ -0,0 +1,37 @@ +package com.baeldung.jpa.generateidvalue; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "app_user") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "user_name") + private String 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; + } + +} \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml index eec7f7cf6e..3bc81910d9 100644 --- a/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml @@ -184,4 +184,29 @@ value="false" /> + + + org.eclipse.persistence.jpa.PersistenceProvider + com.baeldung.jpa.generateidvalue.Admin + com.baeldung.jpa.generateidvalue.Article + com.baeldung.jpa.generateidvalue.IdGenerator + com.baeldung.jpa.generateidvalue.Task + com.baeldung.jpa.generateidvalue.User + true + + + + + + + + + + + + + diff --git a/persistence-modules/java-jpa-2/src/main/resources/primary_key_generator.sql b/persistence-modules/java-jpa-2/src/main/resources/primary_key_generator.sql new file mode 100644 index 0000000000..9acd1bc11b --- /dev/null +++ b/persistence-modules/java-jpa-2/src/main/resources/primary_key_generator.sql @@ -0,0 +1,4 @@ +CREATE SEQUENCE article_seq MINVALUE 1 START WITH 50 INCREMENT BY 50; + +INSERT INTO id_gen (gen_name, gen_val) VALUES ('id_generator', 0); +INSERT INTO id_gen (gen_name, gen_val) VALUES ('task_gen', 10000); \ No newline at end of file diff --git a/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/generateidvalue/PrimaryKeyUnitTest.java b/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/generateidvalue/PrimaryKeyUnitTest.java new file mode 100644 index 0000000000..eead56dbff --- /dev/null +++ b/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/generateidvalue/PrimaryKeyUnitTest.java @@ -0,0 +1,81 @@ +package com.baeldung.jpa.generateidvalue; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * PrimaryKeyGeneratorTest class + * + * @author shiwangzhihe@gmail.com + */ +public class PrimaryKeyUnitTest { + private static EntityManager entityManager; + + @BeforeClass + public static void setup() { + EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2-primarykey"); + entityManager = factory.createEntityManager(); + } + + @Test + public void givenIdentityStrategy_whenCommitTransction_thenReturnPrimaryKey() { + User user = new User(); + user.setName("TestName"); + + entityManager.getTransaction() + .begin(); + entityManager.persist(user); + Assert.assertNull(user.getId()); + entityManager.getTransaction() + .commit(); + + Long expectPrimaryKey = 1L; + Assert.assertEquals(expectPrimaryKey, user.getId()); + } + + @Test + public void givenTableStrategy_whenPersist_thenReturnPrimaryKey() { + Task task = new Task(); + task.setName("Test Task"); + + entityManager.getTransaction() + .begin(); + entityManager.persist(task); + Long expectPrimaryKey = 10000L; + Assert.assertEquals(expectPrimaryKey, task.getId()); + + entityManager.getTransaction() + .commit(); + } + + @Test + public void givenSequenceStrategy_whenPersist_thenReturnPrimaryKey() { + Article article = new Article(); + article.setName("Test Name"); + + entityManager.getTransaction() + .begin(); + entityManager.persist(article); + Long expectPrimaryKey = 51L; + Assert.assertEquals(expectPrimaryKey, article.getId()); + + entityManager.getTransaction() + .commit(); + } + + @Test + public void givenAutoStrategy_whenPersist_thenReturnPrimaryKey() { + Admin admin = new Admin(); + admin.setName("Test Name"); + + entityManager.persist(admin); + + Long expectPrimaryKey = 1L; + Assert.assertEquals(expectPrimaryKey, admin.getId()); + } +} \ No newline at end of file