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

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.

主站蜘蛛池模板: 岳池县| 永泰县| 石林| 安多县| 庆元县| 湟源县| 会东县| 墨玉县| 澄江县| 抚松县| 江山市| 昔阳县| 霍邱县| 鄂托克旗| 兰坪| 邯郸市| 上杭县| 阿克陶县| 六枝特区| 贞丰县| 尚义县| 宝应县| 略阳县| 昭平县| 旺苍县| 津市市| 凌源市| 田东县| 洛南县| 义马市| 灵石县| 左权县| 潞西市| 大新县| 忻州市| 耒阳市| 河北省| 三门县| 鄯善县| 察隅县| 夏邑县|