* BAEL-7490 read write file in separate thread

* Change the to try resources

* Update the code to sync with article

* get next val

* get next val
This commit is contained in:
Wynn Teo 2024-04-12 08:23:17 +08:00 committed by GitHub
parent 2fca380422
commit e34eb87e8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.data.jpa.getnextseq;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mySeqGen")
@SequenceGenerator(name = "mySeqGen", sequenceName = "my_sequence_name", allocationSize = 1)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.spring.data.jpa.getnextseq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyEntityApplication {
public static void main(String[] args) {
SpringApplication.run(MyEntityApplication.class);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.spring.data.jpa.getnextseq;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
@Query(value = "SELECT NEXTVAL('my_sequence_name')", nativeQuery = true)
Long getNextSequenceValue();
}

View File

@ -0,0 +1,22 @@
package com.baeldung.spring.data.jpa.getnextseq;
import java.math.BigInteger;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
@Service
public class MyEntityService {
@PersistenceContext
private EntityManager entityManager;
public Long getNextSequenceValue(String sequenceName) {
BigInteger nextValue = (BigInteger) entityManager.createNativeQuery("SELECT NEXTVAL(:sequenceName)")
.setParameter("sequenceName", sequenceName)
.getSingleResult();
return nextValue.longValue();
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.spring.data.jpa.getnextseq;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
@SpringBootTest
@ActiveProfiles("test")
@Sql(scripts = "/testsequence.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public class MyEntityRepositoryIntegrationTest {
@Autowired
private MyEntityRepository myEntityRepository;
@Autowired
private MyEntityService myEntityService;
@Test
void whenUsingSequenceGenerator_thenNextValueReturned() {
MyEntity entity = new MyEntity();
myEntityRepository.save(entity);
long generatedId = entity.getId();
assertNotNull(generatedId);
assertEquals(1L, generatedId);
}
@Test
void whenUsingCustomQuery_thenNextValueReturned() {
long generatedId = myEntityRepository.getNextSequenceValue();
assertNotNull(generatedId);
assertEquals(1L, generatedId);
}
@Test
void whenUsingEntityManager_thenNextValueReturned() {
long generatedId = myEntityService.getNextSequenceValue("my_sequence_name");
assertNotNull(generatedId);
assertEquals(1L, generatedId);
}
}

View File

@ -0,0 +1,3 @@
DROP SEQUENCE IF EXISTS my_sequence_name;
CREATE SEQUENCE my_sequence_name START 1;