سؤال

I'm using OpenJPA 2.1.0 for my persistence logic with annotations. The primary key in my entities is annotated like the following:

@Id
@GeneratedValue
@Column(name = CONVERSATION_ID)
private long id;

I realized that the generated ids are somehow clustered like 1,2,3,11,12,13,21,22,23,etc.

I learned from the OpenJPA documentation that per default the GeneratorStrategy.AUTO is applied and that the concrete generation strategy is up to the JPA vendor (http://openjpa.apache.org/builds/2.1.0/apache-openjpa-2.1.0/docs/manual/main.html).

Unfortunately I did not find any documentation on the how and why. Can anybody point me to the right direction?

Thanks

هل كانت مفيدة؟

المحلول

When you use the @GeneratedValue annotation without a strategy OpenJPA defaults to AUTO and most databases OpenJPA supports uses Table generation. The default allocationSize is 50, so I would expect to see your keys go something like 1,2,3,4,5,51,52,53,54,101, etc. This is because each time you restart your EntityManagerFactory(or application) OpenJPA must go back to the database to get another batch of keys.

I'm not sure why you're seeing an allocation of 10, but I'm pretty certain that explains what you are seeing. If you were to enable sql trace (openjpa.Log=SQL=trace), you would see OpenJPA going back to the DB to get keys.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top