gpt4 book ai didi

java - @OneToMany 列返回 LazyInitializationException - Spring

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

我的用户 DTO 中有以下映射来存储用户属性的Map:

@Entity(name = "User")
public class User{

@Id
@Column(name = "Id")
private long userId;

@OneToMany
@JoinColumn(name = "properties")
private Map<Long, Property> properties= new LinkedHashMap<Long, LkpAIT>();

}

@Entity(name = "Property")
public class Property{

@Id
@Column(name = "Id")
private long propertyId;

@Column(name = "Name")
private String propertyName;

}

以及下面的@Controller方法

@RequestMapping(value = "/getUser", produces = "application/json", method = RequestMethod.GET)
public @ResponseBody Page<LkpFeed> getLookupFeeds(Model model) {
page = 1;
Page<User> users = userRepo.getUsers(new PageRequest(page, 100)); //returns 100 users
return feeds;
}

@Controller 方法执行正常(找到所有用户),但是当执行 return 语句时,出现以下错误:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myapp.dto.Properties.properties, could not initialize proxy - no Session
org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155)
org.hibernate.collection.internal.PersistentMap.isEmpty(PersistentMap.java:145)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:399)
com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:27)
com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:505)
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100)

根据异常,我的属性字段是导致错误的原因。我已经尝试过 fetchTypes 和 PersistenceContexts ,但目前不知所措。有什么想法吗?

最佳答案

你可以做几件事

  1. @OneToMany中添加fetch Eager

    @Entity(name = "User")
    public class User{

    @Id
    @Column(name = "Id")
    private long userId;

    @OneToMany(fetch=FetchType.EAGER)
    @JoinColumn(name = "properties")
    private Map<Long, Property> properties= new LinkedHashMap<Long, LkpAIT>();

    }
  2. 或者,当您只想列出用户时,如果您不想在返回的 JSON 中包含属性。您可以为 properties 添加 @JsonIgnore

    @Entity(name = "User")
    public class User{

    @Id
    @Column(name = "Id")
    private long userId;

    @JsonIgnore
    @OneToMany
    @JoinColumn(name = "properties")
    private Map<Long, Property> properties= new LinkedHashMap<Long, LkpAIT>();

    }

关于java - @OneToMany 列返回 LazyInitializationException - Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304982/

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