Java toString equals hashCode 方法的重写

来源:互联网 发布:抑郁 知乎 编辑:程序博客网 时间:2024/05/29 10:55

package com.milk.entity;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author admin
 */
@Entity
public class City implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 @Override
 public int hashCode() {
  int hash = 0;
  hash += (id != null ? id.hashCode() : 0);
  return hash;
 }

 @Override
 public boolean equals(Object object) {
   if (!(object instanceof City)) {
   return false;
  }
  City other = (City) object;
  if ((this.id == null && other.id != null)
    || (this.id != null && !this.id.equals(other.id))) {
   return false;
  }
  return true;
 }

 @Override
 public String toString() {
  return "com.milk.entity.City.id=" + id + " ";
 }

}

 

0 0