1

エンティティ クラス

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator1")
@SequenceGenerator(sequenceName = "sequence2", name = "generator1",
allocationSize = 1, initialValue = 1)
private int                id;

主要

EntityManagerFactory emf = Persistence.createEntityManagerFactory("Employee");
                entityManager = emf.createEntityManager();
                 Employee us = new Employee();
                us.setFirstname("John");
                us.setLastname("John");
                entityManager.getTransaction().begin();
                entityManager.persist(us);
                entityManager.getTransaction().commit();

最初の ID 番号は 1 ですが、再度実行すると ID 番号 3 が作成されます (2 のはずです)。その理由はわかりません。ここで何が問題なのですか?

4

1 に答える 1

2

を使用する場合、この動作は論理的ですPostgres。最小 (およびデフォルト) のキャッシュ サイズは1であるため、id を持つエンティティを挿入すると、1プリフェッチされ2ます。

セクション9.16。Postgres マニュアルのSequence Manipulation Functionsは次のように示しています。

Important: To avoid blocking concurrent transactions that obtain numbers from the same sequence, a nextval operation is never rolled back; that is, once a value has been fetched it is considered used, even if the transaction that did the nextval later aborts. This means that aborted transactions might leave unused "holes" in the sequence of assigned values.

つまり、フェッチ2して使用しなくなった場合、再利用されません。

そのため、の3後に表示され1ます。

それにもかかわらず、このような小さいキャッシュ サイズはパフォーマンスに影響を与えるため、推奨されないことに注意してください。

于 2013-01-05T18:04:47.307 に答える