生活随笔
收集整理的這篇文章主要介紹了
JPA中的@MappedSuperclass
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說明地址:http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html
用來申明一個超類,繼承這個類的子類映射時要映射此類中的字段,可以當做是對entity抽取封裝的類。如果子類想重寫此類的映射信息,可以使用?AttributeOverride?and?AssociationOverride?annotations
?
Java代碼 ?
?Example:?Concrete?class?as?a?mapped?superclass??@MappedSuperclass??public?class?Employee?{????????????????@Id???????????protected?Integer?empId;??????????@Version???????????protected?Integer?version;??????????@ManyToOne?@JoinColumn(name="ADDR")??????????protected?Address?address;????????????????public?Integer?getEmpId()?{?...?}??????????public?void?setEmpId(Integer?id)?{?...?}??????????public?Address?getAddress()?{?...?}??????????public?void?setAddress(Address?addr)?{?...?}??}????????@Entity??public?class?FTEmployee?extends?Employee?{??????????????????????????????????????????????????protected?Integer?salary;????????????public?FTEmployee()?{}????????????public?Integer?getSalary()?{?...?}????????????public?void?setSalary(Integer?salary)?{?...?}??}????????????@Entity?@Table(name="PT_EMP")??????@AssociationOverride(name="address",???????joincolumns=@JoinColumn(name="ADDR_ID"))??????public?class?PartTimeEmployee?extends?Employee?{????????????????????????????????????????@Column(name="WAGE")??????????protected?Float?hourlyWage;????????????????public?PartTimeEmployee()?{}????????????????public?Float?getHourlyWage()?{?...?}??????????public?void?setHourlyWage(Float?wage)?{?...?}??????}????????Example:?Non-entity?superclass????????public?class?Cart?{????????????????????????Integer?operationCount;????????????????public?Cart()?{?operationCount?=?0;?}??????????public?Integer?getOperationCount()?{?return?operationCount;?}??????????public?void?incrementOperationCount()?{?operationCount++;?}??????}????????????@Entity??????public?class?ShoppingCart?extends?Cart?{????????????????Collection?items?=?new?Vector();????????????????public?ShoppingCart()?{?super();?}??????????????????...????????????????@OneToMany??????????public?Collection?getItems()?{?return?items;?}??????????public?void?addItem(Item?item)?{??????????????items.add(item);??????????????incrementOperationCount();??????????}??????}?? 分享到:
轉載于:https://www.cnblogs.com/toSeeMyDream/p/4606896.html
總結
以上是生活随笔為你收集整理的JPA中的@MappedSuperclass的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。