Spring引导延迟加载实体未加载所有属性

gre*_*wan 5 java hibernate spring-data-jpa spring-boot mysql-8.0

我有一个Spring Boot 2.1.13(Java 8)项目,其中MySQL 8.0.21包含mysql-connector-java version: 8.0.22以下类。

@Entity
@Table(name = "user")
@JsonIgnoreProperties({"type", "location", "hibernateLazyInitializer", "handler"})
public class User implements java.io.Serializable {
    private UserType userType;
    private Location location;
    private long id;
    private String name;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "usertypeid")
    public UserType getUserType() {
        return this.userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "locationid", nullable = false)
    public Location getLocation() {
        return this.location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


@Entity
@Table(name = "usertype")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class UserType implements java.io.Serializable {
    private long id;
    private String name;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Entity
@Table(name = "location")
public class Location implements java.io.Serializable {
    private long id;
    private String name;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用以下代码获取用户记录

User user = userRepository.findById(userId).orElse(null);
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试从用户对象中获取 UserType 对象时,除了ID之外,我没有得到任何值。例如,当我运行以下命令时,它会给我一个null

user.getUserType().getName()
Run Code Online (Sandbox Code Playgroud)

但是,当我运行以下代码时,我得到了预期的非空值。

user.getLocation().getName()
Run Code Online (Sandbox Code Playgroud)

userType都是location延迟加载的,但是, only 的值location在代码中可用。


  • 我已经检查了数据库表中的值。它们不适null用于用户类型和位置。
  • 我也检查了调试器中的值。该Location对象似乎是一个代理,但不是UserType休眠代理对象。我不确定这是否是一个问题。