loading ...
loading...

2006-08-28 | EJB3的Persistence API学习记录(1)

分享

自定义主键类(primary  key class)需要满足以下几个条件:

  • The access control modifier of the class must be public.
  • The properties of the primary key class must be public or protected if property-based access is used.
  • The class must have a public default constructor.
  • The class must implement the hashCode() and equals(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;
  }
} 
分享 分享 |  评论 (0) |  阅读 (?)  |  固定链接 |  类别 (Java) |  发表于 02:11
搜狐博客温馨提示:搜狐博客官方不会要求参加活动的各位博友缴纳任何的手续费用。请勿轻信留言、评论中的中奖信息,更不要拨打陌生电话及向陌生帐户汇款,谨防受骗!识别更多网络骗术,请 点击查看详情
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
 
  *中国人爱国心,搜狗输入法爱国主题皮肤下载>>
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复