在Object方法中延迟加载的实体:toString,equals和hashCode

时间:2019-01-30 19:04:54

标签: java spring hibernate jpa jackson

我为设备创建了一些模型,但是我不确定我是否进行了正确的映射,而且当我想摆脱急切的加载时,我遇到了错误:

"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\"])",

但是如果我将获取类型更改为渴望,则一切正常。

我的逆向工程架构: enter image description here

然后是我的实体:

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);
    }
}

谢谢您的提示。这是我第一次玩后端。

1 个答案:

答案 0 :(得分:2)

问题是您正在使用实体覆盖toString()equals()hashCode()方法。这些功能中使用的所有内容都必须是基本实体,并与父实体一起加载。这就是为什么在急切加载时没有引发异常的原因。

通常,我不建议使用子实体来确定相等性,以此类推,例如,这要求它们被急切地加载,这不利于性能。为了性能起见,我会延迟加载它们并重写重写的方法,但是如果您需要在这些方法中使用它们,则需要热切地加载它们。

Vlad Mihalcea为实施toString()equalshashCode()写了很好的read