JPA primary key

The following is what I encountered:

---------------- Method A ------------------

@javax.persistence.id  

 

@javax.persistence.SequenceGenerator(name="gen1", sequenceName="SEQTEST1")

@javax.persistence.GeneratedValue(generator="gen1", strategy=GenerationType.AUTO)

@javax.persistence.Column(name="ID")

private long getId() { return id; }

 

---------------- Method B ------------------

@javax.persistence.id  

 

@javax.persistence.SequenceGenerator(name="gen1", sequenceName="SEQTEST1")

@javax.persistence.GeneratedValue(strategy=GenerationType.SEQUENCE, generator="gen1")

@javax.persistence.Column(name="ID")

private long getId() { return id; }

 

Method B does not use SEQTEST1, I don't know where the sequence is used, even if SEQTEST1 is deleted, it will not be affected, and I don't understand the reason. After changing to method A, the value of SEQTEST1 is used.

 

 

The following is reproduced from:

http://blog.csdn.net/love_xsq/article/details/41962611

 

@Table(name = "jdps_content")  

@SequenceGenerator(name="SEQ_JDPS_content",sequenceName="SEQ_JDPS_CONTENT")

publicclassContent {

    @Id

    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_JDPS_content")

    private time;

1. This is one of the entity classes. I use the Oracle database, and the primary key adopts the sequence of oracle, but the problem comes. I found that the growth of my id is not incremented by 1, but incremented by 50. I was very puzzled at that time. Now, I checked the Internet later. It turns out that some people have encountered this problem. It turns out that allocationSize=1 is missing. It turns out that the default increment size of JPA is 50. At the same time, initialValue defaults to 0, which is generally set to initialValue=1. The modified version is as follows: @SequenceGenerator(name="SEQ_JDPS_content",allocationSize=1,initialValue=1, sequenceName="SEQ_JDPS_CONTENT") 

2. There is another issue to pay attention to: whether the annotation is written on the get method or the attribute, this must be unified, and must not be confused. I recommend writing it on the getter method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326395909&siteId=291194637