views:

39

answers:

1

Env: JBoss, (h2, MySQl, postgres), JPA, Hibernate 3.3.x

@Id
@GeneratedValue(strategy = IDENTITY)
private Integer key;
  1. Currently our primary keys are created using the above annotation. We expect to support a large number of users (~million users), what key should be used. Should it be Integer or Long or should I use the unsigned versions of the above declarations.

  2. We have a j2ee application which needs to be populated with some seed data on installation. On purchase, the customer creates his own data on top of the application. We just want to make sure that there is enough room to ship, modify or add data for future releases. What would be the best mechanism to support this, we had looked at starting all table identifiers from a certain id (say 1000) but this mandates modifying primary key generation to have table or sequence based generators and we have around ~100 tables. We are not sure if this is the right strategy for this.

If we use a signed integer approach for the key, would it make sense to have the seed data as everything starting from 0 and below (i.e -ve numbers), so that all customer specific data will be available on 0 and above (i.e. +ve numbers)

+1  A: 

Currently our primary keys are created using the above annotation. We expect to support a large number of users (~million users), what key should be used. Should it be Integer or Long or should I use the unsigned versions of the above declarations

From the Primitive Data Types of the Java tutorials:

int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

Up to you to see if it's safe :)

(...) We just want to make sure that there is enough room to ship, modify or add data for future releases. What would be the best mechanism to support this

I would test this with the mentioned database but using negative IDs for your seed data seems to be the safer solution (no possible collision). If it doesn't work (I think it should), then I believe a TABLE strategy would be the most portable solution.

Pascal Thivent
@Pascal - it does seem to work fine for -ve numbers. What would be a possible case for collision, how does the generated id overflow after it reaches 2,147,483,647 does it move to -2,147,483,648 and start counting towards zero. Just want to make sure that there won't be any issues
@user339108: Well, if you use negative IDs, collision shouldn't be possible which is why this solution is nice. Regarding the other question, I don't think the generator moves to -2,147,483,648 (but I need to check that). If you want to be safe, maybe use a long.
Pascal Thivent
@Pascal - I am not sure about sizing requirements, I would prefer to avoid the Long if possible