gpt4 book ai didi

java - `Object` 方法 : `toString` ,、 `equals` 和 `hashCode` 中延迟加载的实体

转载 作者:行者123 更新时间:2023-12-01 19:43:36 26 4
gpt4 key购买 nike

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

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

但是如果我将获取类型更改为 eager,一切都会正常工作。

我的逆向工程架构: 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);
}
}

谢谢你的提示。这是我第一次尝试后端。

最佳答案

问题在于您正在使用实体重写 toString()equals()hashCode() 方法。这些函数内部使用的所有内容都需要是与父实体一起加载的基本实体。这就是为什么在急切加载时没有抛出异常的原因。

一般来说,我不建议使用子实体来确定相等性等,因为这需要急切地加载它们,这对性能不利。为了性能起见,我会让它们延迟加载并重写重写的方法,但如果您需要在这些方法中使用它们,则需要立即加载它们。

Vlad Mihalcea 写得很好 read关于实现 toString()equalshashCode()

关于java - `Object` 方法 : `toString` ,、 `equals` 和 `hashCode` 中延迟加载的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54447810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com