解決JPA懶載入的辦法
阿新 • • 發佈:2019-01-05
方案1:
1.1、在多的一方的類上加上@ToString()註解,重寫toString方法
1.2、在程式碼中獲取一的時候主動呼叫多的toString()方法:
uopUser.getUserChannelRelationList().toString();
方案2:
2.1、在程式碼中主動get多的一方的唯一主鍵:uopUser.getUserChannelRelationList().get[0].getId();
1、一對多的類
import lombok.*; import javax.persistence.*; import java.util.List; @ToString() @Entity @Getter @Setter @Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "uop_user") public class UopUser { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String userName; /** * 渠道 */ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy = "uopUser") private List<UserChannelRelation> userChannelRelationList; /** * 工業分類 */ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy = "uopUser") private List<UserCategoryRelation> userCategoryRelationList; /** * 分部 */ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy = "uopUser") private List<UserERPOrganizationFBRelation> userERPOrganizationFBRelationList; /** * 經營部門 */ @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy = "uopUser") private List<UserERPOrganizationJYRelation> userERPOrganizationJYRelationList; }
2、多的一方
import com.alibaba.fastjson.annotation.JSONType; import com.efivestar.weaf.security.domain.ManageUser; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.*; import org.springframework.validation.annotation.Validated; import javax.persistence.*; import java.util.Date; @ToString(exclude = "uopUser") @JSONType(ignores = "uopUser") @Entity @Getter @Setter @Builder @NoArgsConstructor @AllArgsConstructor @Table(name = "user_channel_relation") public class UserChannelRelation { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; /** * 使用者 */ @JsonIgnore // @JsonBackReference @ManyToOne @JoinColumn(name = "uop_user_id") private UopUser uopUser; /** * 渠道 */ @JsonIgnore @OneToOne(fetch = FetchType.LAZY) private Channel channel; /** * 可選狀態 */ @Column(nullable = true) @Builder.Default private boolean checkFlag = false; /** * 修改時間 */ @Column(name = "update_time") private Date updateTime; }