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

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.

主站蜘蛛池模板: 永登县| 沾益县| 会泽县| 闽清县| 鄂托克旗| 洛川县| 北票市| 武威市| 饶河县| 津市市| 夏邑县| 仪征市| 丰县| 邢台市| 女性| 清涧县| 米林县| 德惠市| 平遥县| 綦江县| 宜宾市| 唐山市| 南溪县| 大宁县| 视频| 莱西市| 康乐县| 平潭县| 嘉荫县| 嘉荫县| 枣强县| 永福县| 麦盖提县| 渭源县| 昌江| 巴彦淖尔市| 长汀县| 仁化县| 上饶市| 宁德市| 府谷县|