简体   繁体   中英

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.

@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). Otherwise Its fine, But I need to use this to send it to the frontend.

Following is the game 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

@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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM