当前位置: 首页 > 知识库问答 >
问题:

在将object转换为json期间,未能懒惰地初始化角色集合

通沛
2023-03-14

我是spring的新手,在从一个与其他表有关系的表中获取记录时,会出现这个懒洋洋的初始化错误。我在网上读了很多,但没有找到一个合适的方法。

表1:

@SuppressWarnings("serial")
@Entity
public class Terminal extends BaseEntity {

@Column(length = 100, unique = true)
private String shortName;

@Column
private short number; // short stores up to 32767 value

@Column
private String description;     

@OneToMany
@JoinColumn(name = "terminal_id", referencedColumnName = "uuid")
@Cascade({ CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DELETE })
private Set<BusinessHour> businessHour;

 public String getShortName() {
    return shortName;
}

public void setShortName(String shortName) {
    this.shortName = shortName;
}

public short getNumber() {
    return number;
}

public void setNumber(short number) {
    this.number = number;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
public Set<BusinessHour> getBusinessHour() {
    return businessHour;
}

public void setBusinessHour(Set<BusinessHour> businessHour) {
    this.businessHour = businessHour;
}
@SuppressWarnings("serial")
@Entity
public class BusinessHour extends BaseEntity {

@Column
private DayOfWeek dayOfWeek;

@Column
private LocalTime startOfOperation;

@Column
private LocalTime endOfOperation;

public DayOfWeek getDayOfWeek() {
    return dayOfWeek;
}
}
@Service
public class TerminalServiceImpl implements TerminalService {

@Autowired
TerminalRepository terminalRepository;


    Iterable<Terminal> allTerminals = terminalRepository.findAll();
    List<Terminal> terminalList =  new ArrayList<Terminal>();
    for (Terminal terminal : allTerminals) {
        terminalList.add(terminal);
    }
    return terminalList;
}
@Transactional
public interface TerminalRepository extends CrudRepository<Terminal, Long> {
}
private List<Terminal> updateTerminalList() {
    List<Terminal> allTerminals = terminalService.fetchAllTerminal();
    return allTerminals;
}

public void terminalWrapperRun() {
    try {
       Payload payload = createTerminalPayload(applicationId);
        String json3 = object2Json(payload);
        kafkaRESTUtils.sendServerPayload(json3);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

 public String object2Json(Object dataArray) throws JsonProcessingException {
    return mapper.writeValueAsString(dataArray);
}

错误:

com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: terminal.model.Terminal.businessHour, could not initialize proxy - no Session (through reference chain: 

将获取对象转换为JSON时获取异常。我发现这是由于代理对象返回由于fetch类型lazy(我希望保持原样)。

共有1个答案

井镜
2023-03-14

我认为这个问题与ORM默认情况下延迟加载集合有关。

@OneToMany
@JoinColumn(name = "terminal_id", referencedColumnName = "uuid")
@Cascade({ CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DELETE })
private Set<BusinessHour> businessHour;

@OneTomany注释有一个fetch属性,默认设置为LAZY。

FetchType fetch() default LAZY;

单胞菌参比

在ORM会话中执行JSON生成(我只建议这样做是导致性能问题的第一个选项)。

 类似资料: