Hibernate loads a lazy proxy, but I only need a PC

I have the following objects:

@Entity
public class Room {

    @ManyToOne(optional=true,fetch=FetchType.LAZY)
    private Player player1;

    ...

}

@Entity
public class Player {

    @Id
    @Column(updatable=false)
    private long id;

    public long getId() {
        return id;
    }

    ...

}

Now this statement is inside Room...

player1.getId();

... will cause the entire object to Playerbe retrieved from the database. However, I only need its primary key id, which it should already have (otherwise how can it get the data?).

How can I access a lazy proxy server Player idwithout starting access to the database?

+5
source share
3 answers

, Session. getIdentifier() Session, . , .

+4

@Column private long playerId Room. , Player .

, playerId ( ..), , , , .

edit: @Adam Dyga, , false

@Entity
public class Room {

    @ManyToOne(optional=true,fetch=FetchType.LAZY)
    private Player player1;
    @Column(name="id", insertable = false, updatable = false) 
    private long playerId
    ...

}
+1

I found a simpler solution that should do getId() final. This will prevent Hibernate from switching with a lazy action in the proxy, so it will simply return the already-known identifier without causing any problems.

0
source

All Articles