简体   繁体   English

DefaultExceptionListener:未能延迟初始化角色集合无法初始化代理 - 否 Session

[英]DefaultExceptionListener : failed to lazily initialize a collection of role could not initialize proxy - no Session

I am getting the error failed to lazily initialize a collection of role could not initialize proxy - no Session when i am trying to retrieve a list from my database object.当我尝试从我的数据库 object 中检索列表时,我收到错误未能延迟初始化角色集合无法初始化代理 - 没有 Session。

@Transactional
private DataListener<String> onGetPieceInfoEvent() {
return (senderClient, data, ack) -> {

        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(data);
        String roomId = jsonNode.get("roomId").textValue();
        String color = jsonNode.get("color").textValue();
    
        Game game = this.gameService.findByRoomId(roomId);
    
        List<Piece> pieces = color.equals("white") ? game.getWhitePieces()
        : game.getBlackPieces();
    
        Collection<SocketIOClient> clientsCollection =   this.server.getRoomOperations(roomId).getClients();
    
    
        ArrayList<SocketIOClient> clients = new ArrayList<>(clientsCollection);
    
        // SocketIOClient client1 = senderClient; // the client from where the data came 
    
        // the client to whom we need to send the converted data.
        SocketIOClient client = clients.get(0).getSessionId().equals(senderClient.getSessionId()) ? clients.get(1)
                : clients.get(0);
    
        socketService.sendMessage(roomId, "colorOnMount",mapper.writeValueAsString(pieces),
                client);
    }

I get the error when I use mapper.writeValueAsString(pieces).使用 mapper.writeValueAsString(pieces) 时出现错误。 Otherwise Its fine, But I need to use this to send it to the frontend.否则没关系,但我需要用它把它发送到前端。

Following is the game Class.以下是游戏 Class。

@Entity
@Table(name="game_data")
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="game_id")
private Integer gameId;

    @Column(name="room",nullable = false)
    private String roomId;
    
    GameState state;
    
    @ElementCollection
    @ColumnDefault("[]")
    @CollectionTable(name = "white_pieces", joinColumns = @JoinColumn(name = "game_id"))
    @Basic(fetch = FetchType.EAGER)
    private List<Piece> whitePieces = new ArrayList<>();
    
    @ElementCollection
    @ColumnDefault("[]")
    @CollectionTable(name = "black_pieces", joinColumns = @JoinColumn(name = "game_id"))
    @Basic(fetch = FetchType.EAGER)
    private List<Piece> blackPieces = new ArrayList<>();
}

Following is the Piece Class以下是作品 Class

@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class Piece {
@JsonProperty("image")
@Column
private String image;

@JsonProperty("x")
@Column
private Integer x;

@JsonProperty("y")
@Column
private Integer y;

@JsonProperty("type")
@Column
private String type;

@JsonProperty("team")
@Column
private String team;

@JsonProperty("enPassant")
@Column
private Boolean enPassant;

For lazy loading to work you need a session which is bound to the transaction where the entity was loaded or persisted.为了延迟加载工作,你需要一个 session 绑定到实体被加载或持久化的事务。

You are running the code outside that transaction and therefore get the exception.您正在该事务之外运行代码,因此会出现异常。

Note that you construct the lambda inside a transaction but execution happens elsewhere outside that transaction.请注意,您在事务内部构造了 lambda,但执行发生在该事务之外的其他地方。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法延迟初始化角色集合无法初始化代理-无会话 - Failed to lazily initialize a collection of role could not initialize proxy - no Session 无法延迟初始化角色集合:无法初始化代理-无会话 - failed to lazily initialize a collection of role : could not initialize proxy - no Session 休眠-无法延迟初始化角色集合:无法初始化代理-没有会话 - Hibernate - failed to lazily initialize a collection of role: could not initialize proxy - no Session 无法延迟初始化角色[]的集合,无法初始化代理-没有会话 - failed to lazily initialize a collection of role [], could not initialize proxy - no Session Hibernate 无法延迟初始化角色集合 无法初始化代理 - 没有 Session - Hibernate failed to lazily initialize a collection of role could not initialize proxy - no Session HTTP 状态 500 - 无法延迟初始化角色集合:...,无法初始化代理 - 没有会话 - HTTP Status 500 - failed to lazily initialize a collection of role: ..., could not initialize proxy - no Session 面对未能延迟初始化集合,并且无法初始化代理-无会话 - facing failed to lazily initialize a collection and could not initialize proxy - no Session 未能懒惰地初始化角色集合,..无法初始化代理 - 没有会话 - JPA + SPRING - failed to lazily initialize a collection of role,..could not initialize proxy - no Session - JPA + SPRING LazyInitializationException:无法延迟初始化角色集合:Usuario.autorizacoes,无法初始化代理-没有会话 - LazyInitializationException: failed to lazily initialize a collection of role: Usuario.autorizacoes, could not initialize proxy - no Session Hibernate:LazyInitializationException:懒得初始化一个角色集合。 无法初始化代理 - 没有会话 - Hibernate: LazyInitializationException: failed to lazily initialize a collection of role. Could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM