自定义主键类(primary key class)需要满足以下几个条件:
- The access control modifier of the class must be
public. - The properties of the primary key class must be
publicorprotectedif property-based access is used. - The class must have a public default constructor.
- The class must implement the
hashCode()andequals(Object other)methods. - The class must be serializable.
- A composite primary key must be represented and mapped to multiple fields or properties of the entity class, or must be represented and mapped as an embeddable class.
- If the class is mapped to multiple fields or properties of the entity class, the names and types of the primary key fields or properties in the primary key class must match those of the entity class.
和hibernate相比,比较奇怪的是属性必须声明为public或者protect,这个类的样子不符合JavaBean的规范了,有点奇特.简单例子:
public final class LineItemKey implements Serializable {
public Integer orderId;
public int itemId;
public LineItemKey() {}
public LineItemKey(Integer orderId, int itemId) {
this.orderId = orderId;
this.itemId = itemId;
}
public boolean equals(Object otherOb) {
if (this == otherOb) {
return true;
}
if (!(otherOb instanceof LineItemKey)) {
return false;
}
LineItemKey other = (LineItemKey) otherOb;
return (
(orderId==null?other.orderId==null:orderId.equals
(other.orderId)
)
&&
(itemId == other.itemId)
);
}
public int hashCode() {
return (
(orderId==null?0:orderId.hashCode())
^
((int) itemId)
);
}
public String toString() {
return "" + orderId + "-" + itemId;
}
}
![]() |
谢亚龙逼女足姑娘作检讨(图)
“安静”为啥成裁判口头语?
姚明私下发给刘翔的短信
|
![]() |
曝光:姚明小时候与可爱女生合影(图) 组图:隋菲菲私家相册 率性美感领衔女篮 |
![]() |
![]() |
![]() |


档案
日志
相册
视频








评论
想第一时间抢沙发么?