官术网_书友最值得收藏!

Primary keys

A primary key field is mapped by using the @Id annotation as in the following example:

@Entity 
public class Movie { 
    @Id 
    private long id; 
} 
Note that the primary key field itself should not be named Id; this is just a very common situation where we use a field with the name id as the primary key of our table. However, the primary key field can be named anything.

You can also use the primary key field on a getter method in the following example:

@Entity 
public class Movie { 
     
private long id; 
 
    @Id 
public long getId() { 
        return id; 
    } 
 
} 

The difference between the two approaches is that if you used the @Id annotation on a class field directly, the persistence provider will read all other mapping annotations from class fields. Otherwise, it will read all other mappings from the getter methods. You cannot mix annotations on both fields and getters, and the @Id annotation is a descriminator here. The choice between either approaches is the developer's preference. I personally prefer using field mapping in order to make all mapping information available at the beginning of the class.

主站蜘蛛池模板: 山东| 雷波县| 大宁县| 额敏县| 绍兴县| 夏邑县| 巴塘县| 江孜县| 汽车| 白银市| 连山| 绥宁县| 建宁县| 锡林浩特市| 锦州市| 黎城县| 施甸县| 眉山市| 太仆寺旗| 石林| 汉阴县| 和林格尔县| 望谟县| 明水县| 六枝特区| 广水市| 济源市| 长治县| 大悟县| 石景山区| 开阳县| 廊坊市| 青岛市| 荣昌县| 亚东县| 岚皋县| 河间市| 邹城市| 阳朔县| 南丹县| 毕节市|