0

I try to make a project using hibernate and postgres as DB. The problem I have is I need to store the primary key as this 22/2017 or like 432/1990.

Let's say the first number is object_id and second year_added.

I think what I want to achieve is to make a first number and second number together a primary key so 22/2017 is different from 22/2016. The only idea I have is when user add new object I generate current date year and trying to find last id and increment it.

So next year first added object should be : 1/2018.

So far in my db only object_id is stored as a primary key.

lucaskos
  • 67
  • 9
  • Do you need to store the PK "as this", which is a String, or can you consider using a composite key with two integers? – Guillaume F. Jun 23 '17 at 21:13
  • Create a primary key with two integer columns –  Jun 23 '17 at 23:08
  • Yes composite key is what I ment. So i need to decrlare a key id that autoincrement and year that is generates from insertion date? How to achieve it? – lucaskos Jun 24 '17 at 05:36

1 Answers1

0

This solution seems to work fine: PostgreSQL: Auto-increment based on multi-column unique constraint

Thanks for helping me anyway.

lucaskos
  • 67
  • 9