* add primary key demo

* add primary key

* use eclipse link

* update unit test

* refact package name

* format code with space

Co-authored-by: joe zhang <joe.zhang@ringcentral.com>
This commit is contained in:
Joe Zhang 2020-06-12 22:18:54 +08:00 committed by GitHub
parent 0d9e691fe4
commit c529170a43
8 changed files with 309 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -184,4 +184,29 @@
value="false" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-h2-primarykey">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.baeldung.jpa.generateidvalue.Admin</class>
<class>com.baeldung.jpa.generateidvalue.Article</class>
<class>com.baeldung.jpa.generateidvalue.IdGenerator</class>
<class>com.baeldung.jpa.generateidvalue.Task</class>
<class>com.baeldung.jpa.generateidvalue.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.sql-load-script-source"
value="primary_key_generator.sql" />
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.jdbc.allow-native-sql-queries" value="true" />
</properties>
</persistence-unit>
</persistence>

View File

@ -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);

View File

@ -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());
}
}