Lombok 是一个非常受欢迎和有用的图书馆。尽管如此,请注意Lombok @EqualsAndHashCode对实体的影响可能会带来严重问题。
实体应实施equals()和hashCode()。主要问题是Hibernate要求实体在其所有状态转换(瞬态,附加,分离 和删除)中等于自身。使用Lombok @EqualsAndHashCode不会尊重此要求。
避免下面这些方法 :
1. 避免使用的Lombok默认行为@EqualsAndHashCode:
@Entity @EqualsAndHashCode <b>public</b> <b>class</b> LombokDefaultProduct implements Serializable { <b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) <b>private</b> Long id; <b>private</b> String name; <b>private</b> String code;
2. 避免使用只有主键的Lombok@EqualsAndHashCode
@Entity @EqualsAndHashCode(exclude = {<font>"name"</font><font>, </font><font>"code"</font><font>}) <b>public</b> <b>class</b> LombokIdProduct implements Serializable { <b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) <b>private</b> Long id; <b>private</b> String name; <b>private</b> String code; </font>
3. 避免使用默认equals()和hashCode(),下面实体没有编写自己的这两个方法,使用的是默认,不推荐这样做:
@Entity <b>public</b> <b>class</b> DefaultProduct implements Serializable { <b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) <b>private</b> Long id; <b>private</b> String name; <b>private</b> String code; <b>public</b> Long getId() { <b>return</b> id; } <b>public</b> <b>void</b> setId(Long id) { <b>this</b>.id = id; } <b>public</b> String getName() { <b>return</b> name; } <b>public</b> <b>void</b> setName(String name) { <b>this</b>.name = name; } <b>public</b> String getCode() { <b>return</b> code; } <b>public</b> <b>void</b> setCode(String code) { <b>this</b>.code = code; } }
4. 避免使用只依赖主键的equals()和hashCode():
@Entity <b>public</b> <b>class</b> IdProduct implements Serializable { <b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) <b>private</b> Long id; <b>private</b> String name; <b>private</b> String code; <b>public</b> Long getId() { <b>return</b> id; } <b>public</b> <b>void</b> setId(Long id) { <b>this</b>.id = id; } <b>public</b> String getName() { <b>return</b> name; } <b>public</b> <b>void</b> setName(String name) { <b>this</b>.name = name; } <b>public</b> String getCode() { <b>return</b> code; } <b>public</b> <b>void</b> setCode(String code) { <b>this</b>.code = code; } @Override <b>public</b> <b>int</b> hashCode() { <b>int</b> hash = 3; hash = 89 * hash + Objects.hashCode(<b>this</b>.id); <b>return</b> hash; } @Override <b>public</b> <b>boolean</b> equals(Object obj) { <b>if</b> (<b>this</b> == obj) { <b>return</b> <b>true</b>; } <b>if</b> (obj == <b>null</b>) { <b>return</b> false; } <b>if</b> (getClass() != obj.getClass()) { <b>return</b> false; } <b>final</b> IdProduct other = (IdProduct) obj; <b>if</b> (!Objects.equals(<b>this</b>.id, other.id)) { <b>return</b> false; } <b>return</b> <b>true</b>; } }
推荐这些方法: 1. 依靠@NaturalId
@Entity <b>public</b> <b>class</b> NaturalIdProduct implements Serializable { <b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) <b>private</b> Long id; <b>private</b> String name; @NaturalId @Column(nullable = false, updatable = false, unique = <b>true</b>, length = 50) <b>private</b> String code; <b>public</b> Long getId() { <b>return</b> id; } <b>public</b> <b>void</b> setId(Long id) { <b>this</b>.id = id; } <b>public</b> String getName() { <b>return</b> name; } <b>public</b> <b>void</b> setName(String name) { <b>this</b>.name = name; } <b>public</b> String getCode() { <b>return</b> code; } <b>public</b> <b>void</b> setCode(String code) { <b>this</b>.code = code; } @Override <b>public</b> <b>boolean</b> equals(Object o) { <b>if</b> (<b>this</b> == o) { <b>return</b> <b>true</b>; } <b>if</b> (!(o instanceof NaturalIdProduct)) { <b>return</b> false; } NaturalIdProduct naturalIdProduct = (NaturalIdProduct) o; <b>return</b> Objects.equals(getCode(), naturalIdProduct.getCode()); } @Override <b>public</b> <b>int</b> hashCode() { <b>return</b> Objects.hash(getCode()); } }
2. 依靠主键:
@Entity <b>public</b> <b>class</b> GoodProduct implements Serializable { <b>private</b> <b>static</b> <b>final</b> <b>long</b> serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) <b>private</b> Long id; <b>private</b> String name; <b>private</b> String code; <b>public</b> Long getId() { <b>return</b> id; } <b>public</b> <b>void</b> setId(Long id) { <b>this</b>.id = id; } <b>public</b> String getName() { <b>return</b> name; } <b>public</b> <b>void</b> setName(String name) { <b>this</b>.name = name; } <b>public</b> String getCode() { <b>return</b> code; } <b>public</b> <b>void</b> setCode(String code) { <b>this</b>.code = code; } @Override <b>public</b> <b>boolean</b> equals(Object obj) { <b>if</b> (<b>this</b> == obj) { <b>return</b> <b>true</b>; } <b>if</b> (!(obj instanceof GoodProduct)) { <b>return</b> false; } <b>return</b> id != <b>null</b> && id.equals(((GoodProduct) obj).id); } @Override <b>public</b> <b>int</b> hashCode() { <b>return</b> 2018; } }
良好的equals()和hashCode():
源代码可以在 这里 找到