欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

解决JPA懒加载的办法,jpa加载

来源: javaer 分享于  点击 1042 次 点评:92

解决JPA懒加载的办法,jpa加载


方案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;
}

相关文章

    暂无相关文章

用户点评