Custom Composite key / Manged Version (SpringBoot, Spring JPA)

Viewed 24

I have following problem/"challenge". I have an Entity called Epis, that has a composite key made out from an Id and a Quartal.

    @Entity(name= "Epis")
    @Table(name= "epis")
    @Getter
    @Setter
    @NoArgsConstructor
    @IdClass(value = EpisId.class)
    public class Epis implements Serializable {
    SequenceGenerator(name = "generator_epis_ID", initialValue = 2000000)
    GeneratedValue(strategy = GenerationType.SEQUENCE,  generator = "generator_epis_ID")
    @Id
    private Long id;
    @Id
    private Quartal qt; //Version
    private Long pid;

Quartal should work like a version. For Quartal I am using a ENUM [Q0, Q1, Q2, Q3, Q4]. Q0 is before the year starts. At the begining of each Quartal each Row/item should be duplicated with a new "version". So at the end of the year I have 5 Versions of an Item with the same Id. All other changes made to an item throughout the year are not important. For example I have an Item with Composite key:

id= 2000000
Quartal= Q0
data=...

at the begining of the year a new item schould be inserted

id=2000000
Quartal=Q1
data=...
  1. Is there a way I can do this out of the box with Spring jpa?

When Save I save a new Item

    returnedEpis=episServiceImpl.save(new Epis(2000000, Q1, ....);

The id will be overwriten with the next value of the Sequence (example 2000432). If I remove the SequenceGenerator and GeneratedValue, and manage the Id Sequence outside of the this Entity.

  1. How can I get an unused number? Or a number in a sequence witch is not "coupled" to this entity?

  2. Any other suggestion or ideas how to resolve this are very much appriciated.

Custom Composite key / Manged Version (SpringBoot, Spring JPA)

0 Answers
Related