![](/img/trans.png)
[英]Question regarding Hashcode and Equals and toString method in Entities
[英]Lazily loaded entities in `Object` methods: `toString`, `equals` and `hashCode`
我為設備創建了一些模型,但我不確定我是否做了正確的映射,而且當我想擺脫急切加載時,我遇到了錯誤:
"Type definition error: [simple type, class
org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested
exception is
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No
serializer found for class
org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no
properties discovered to create BeanSerializer (to avoid exception,
disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference
chain:
java.util.ArrayList[0]-com.winterrent.winterrent.entity.ItemProperty[\"item\"]-com.winterrent.winterrent.entity.Item$HibernateProxy$RO0mkQSh[\"hibernateLazyInitializer\"])",
但是如果我將獲取類型更改為渴望一切正常。
然后我的實體:
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "item")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Enumerated(EnumType.STRING)
@Column(name = "type")
private ItemType itemType;
public Item() {
}
public Item(ItemType itemType) {
this.itemType = itemType;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ItemType getItemType() {
return itemType;
}
public void setItemType(ItemType itemType) {
this.itemType = itemType;
}
@Override
public String toString() {
return "Item{" +
"id=" + id +
", itemType=" + itemType +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return id == item.id &&
Objects.equals(itemType, item.itemType);
}
@Override
public int hashCode() {
return Objects.hash(id, itemType);
}
}
2)
public enum ItemType {
SKI, BOARD
}
3)
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "item_property_definition")
public class ItemPropertyDefinition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "property_name")
private String propertyName;
@Column(name = "type")
@Enumerated(EnumType.STRING)
private ItemType itemType;
public ItemPropertyDefinition() {
}
public ItemPropertyDefinition(String propertyName, ItemType itemType) {
this.propertyName = propertyName;
this.itemType = itemType;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPropertyName() {
return propertyName;
}
public void setPropertyName(String propertyName) {
this.propertyName = propertyName;
}
public ItemType getItemType() {
return itemType;
}
public void setItemType(ItemType itemType) {
this.itemType = itemType;
}
@Override
public String toString() {
return "ItemPropertyDefinition{" +
"id=" + id +
", propertyName='" + propertyName + '\'' +
", itemType=" + itemType +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemPropertyDefinition that = (ItemPropertyDefinition) o;
return id == that.id &&
Objects.equals(propertyName, that.propertyName) &&
Objects.equals(itemType, that.itemType);
}
@Override
public int hashCode() {
return Objects.hash(id, propertyName, itemType);
}
}
最后映射:
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "item_properties")
public class ItemProperty {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Item item;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "item_property_definition_id")
private ItemPropertyDefinition itemPropertyDefinition;
@Column(name = "value")
private String value;
public ItemProperty(){}
public ItemProperty(Item item, ItemPropertyDefinition itemPropertyDefinition, String value) {
this.item = item;
this.itemPropertyDefinition = itemPropertyDefinition;
this.value = value;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public ItemPropertyDefinition getItemPropertyDefinition() {
return itemPropertyDefinition;
}
public void setItemPropertyDefinition(ItemPropertyDefinition itemPropertyDefinition) {
this.itemPropertyDefinition = itemPropertyDefinition;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "ItemProperty{" +
"id=" + id +
", item=" + item +
", itemPropertyDefinition=" + itemPropertyDefinition +
", value='" + value + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ItemProperty that = (ItemProperty) o;
return id == that.id &&
Objects.equals(item, that.item) &&
Objects.equals(itemPropertyDefinition, that.itemPropertyDefinition) &&
Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(id, item, itemPropertyDefinition, value);
}
}
謝謝你的提示。 這是我第一次玩后端。
問題是您正在使用實體覆蓋toString()
、 equals()
和hashCode()
方法。 在這些函數中使用的一切都需要是基本實體,與父實體一起加載。 這就是為什么在急切加載時沒有拋出異常的原因。
一般來說,我不建議使用子實體來確定相等性等等,例如,這需要急切地加載它們,這對性能不利。 出於性能考慮,我會讓它們延遲加載並重寫覆蓋的方法,但是如果您需要在這些方法中使用它們,則需要急切地加載它們。
弗拉德·米哈爾西亞寫了一個良好的閱讀有關實現toString()
equals
和hashCode()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.