second entity with SequenceGenerator added and test written

This commit is contained in:
Thomas Fellner
2021-08-13 08:36:25 +02:00
parent 531024b8b4
commit 9aa2fc824a
3 changed files with 80 additions and 15 deletions
@@ -8,7 +8,8 @@ import io.ebean.config.dbplatform.IdType;
import io.ebeaninternal.api.SpiEbeanServer;
import org.junit.Test;
import org.tests.idkeys.db.GenKeyIdentity;
import org.tests.idkeys.db.GenKeySequence;
import org.tests.idkeys.db.GenKeySeqA;
import org.tests.idkeys.db.GenKeySeqB;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -17,29 +18,48 @@ import java.sql.Statement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue;
public class TestGeneratedKeys extends BaseTestCase {
@Test
@ForPlatform(Platform.H2) // readSequenceValue is H2 specific
public void testSequence() throws SQLException {
public void testGenKeySeqA() throws SQLException {
assumeTrue(idType() == IdType.SEQUENCE);
SpiEbeanServer server = spiEbeanServer();
if (idType() != IdType.SEQUENCE) {
// only run this test when SEQUENCE is being used
return;
}
try (Transaction tx = server.beginTransaction()) {
long sequenceStart = readSequenceValue(tx, GenKeySequence.SEQUENCE_NAME);
long sequenceStart = readSequenceValue(tx, GenKeySeqA.SEQUENCE_NAME);
GenKeySequence al = new GenKeySequence();
GenKeySeqA al = new GenKeySeqA();
al.setDescription("my description");
server.save(al);
long sequenceCurrent = readSequenceValue(tx, GenKeySeqA.SEQUENCE_NAME);
long sequenceCurrent = readSequenceValue(tx, GenKeySequence.SEQUENCE_NAME);
assertNotNull(al.getId());
assertFalse(sequenceStart == sequenceCurrent);
assertEquals(sequenceStart + 20, sequenceCurrent);
}
}
@Test
@ForPlatform(Platform.H2) // readSequenceValue is H2 specific
public void testGenKeySeqB() throws SQLException {
assumeTrue(idType() == IdType.SEQUENCE);
SpiEbeanServer server = spiEbeanServer();
try (Transaction tx = server.beginTransaction()) {
long sequenceStart = readSequenceValue(tx, GenKeySeqB.SEQUENCE_NAME);
GenKeySeqB al = new GenKeySeqB();
al.setDescription("my description");
server.save(al);
long sequenceCurrent = readSequenceValue(tx, GenKeySeqB.SEQUENCE_NAME);
assertNotNull(al.getId());
assertFalse(sequenceStart == sequenceCurrent);
@@ -4,15 +4,18 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
public class GenKeySequence {
public final static String SEQUENCE_NAME = "SEQ";
public class GenKeySeqA {
public final static String SEQUENCE_NAME = "GEN_KEY_A_SEQ_NAME";
/**
* {@link GeneratedValue#generator()} is not empty, but no {@code SequenceGenerator} is present.
* So the sequence is named like the generator: {@link #SEQUENCE_NAME}.
*/
@Id
@SequenceGenerator(name = "SEQ_NAME", sequenceName = GenKeySequence.SEQUENCE_NAME)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_NAME")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE_NAME)
private Long id;
private String description;
@@ -0,0 +1,42 @@
package org.tests.idkeys.db;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
public class GenKeySeqB {
public final static String SEQUENCE_NAME = "GEN_KEY_B_SEQ_NAME";
private final static String SEQ_GEN_NAME = "SEQ_GEN_NAME";
/**
* {@link GeneratedValue#generator()} links to {@link SequenceGenerator#name()}.
* The name of the sequence is {@link SequenceGenerator#sequenceName()}.
*/
@Id
@SequenceGenerator(name = SEQ_GEN_NAME, sequenceName = GenKeySeqB.SEQUENCE_NAME)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQ_GEN_NAME)
private Long id;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}