Java 使用联接表避免jpa获取

Java 使用联接表避免jpa获取,java,jpa,Java,Jpa,我有接下来的课程: 类用户: @Entity @JsonRootName(value = "user") @Table(name = "web_users", schema = "t_dw_comercial") public class User { @Id private int userId; private String fullName; private String ldapId; private String email; @Man

我有接下来的课程:

类用户:

@Entity
@JsonRootName(value = "user")
@Table(name = "web_users", schema = "t_dw_comercial")
public class User {


   @Id
   private int userId;

   private String fullName;

   private String ldapId;

   private String email;


   @ManyToOne
    @JoinColumn(name= "u_type", nullable=false)
    private Role uType;

    private String deleteFlag;

    private String typeAccess;

    @OneToMany(fetch=FetchType.LAZY)
    @JoinTable(name="web_users_roles",
    joinColumns = {@JoinColumn(name="user_id")},
    inverseJoinColumns = {@JoinColumn(name="role_id")}
    )
    private List<Role> roles;

}
存储库:

@Repository
public interface UserRepository extends CrudRepository<User, Long>{

    @Query("SELECT u FROM User u where u.ldapId= ?1")
public User findUserByLdapId(String loginName);


}

提前谢谢

我不确定我是否正确理解了您的问题,因为Jpa/Hibernate默认会这样做。默认情况下,所有*ToOne关系都是渴望的,而默认情况下,所有*ToMany关系都是懒惰的

来自
@OneToMany
的文档

public abstract FetchType fetch

    (Optional) Whether the association should be lazily loaded or must be eagerly
    fetched. The EAGER strategy is a requirement on the persistence provider runtime 
    that the associated entities must be eagerly fetched. The LAZY strategy is a 
    hint to the persistence provider runtime.

Default:
    javax.persistence.FetchType.LAZY
@ManyToOne

public abstract FetchType fetch

   (Optional) Whether the association should be lazily loaded or must be eagerly 
   fetched. The EAGER strategy is a requirement on the persistence provider runtime
   that the associated entity must be eagerly fetched. The LAZY strategy is a hint 
   to the persistence provider runtime.

Default:
   javax.persistence.FetchType.EAGER

我们打算这样做。上次我检查时,它在JPA中,但在Hibernate中不可用。它现在似乎在Hibernate中可用。我知道,但即使有两个注释,对于同一类的不同属性,我也会急切地获取所有数据。这非常非常奇怪。我想知道代码中是否有其他东西迫使集合被急切地加载(例如,迭代集合的东西)。能否启用SQL语句的日志记录并调试加载
用户的JPA调用
?如果在那里看不到具有
角色的join.user\u id
,则可以确定有其他内容正在强制加载集合。可能是@joinedTable吗?我找不到任何其他的可能性,除了在集合中迭代的一段代码之外,我想不出还有什么其他的可能性,结果是强制加载它。我不认为
@joinedTable
是罪魁祸首,因为这个注释不应该影响懒惰/急切的负载(据我所知)。我编辑并添加了其他类、控制器、服务和存储库,这就是我所拥有的。
@Controller
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @CrossOrigin
    @RequestMapping(value = "/dashboard", params = {"user"}, method = RequestMethod.GET,  produces = "application/json")
    public ResponseEntity<User>  getUser(@RequestParam(value = "user") String ldapId) {

        User user =  userService.findUserByLdapId(ldapId);

        if(user == null)
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);

        return new ResponseEntity<>(user, HttpStatus.OK);

    };
}
{
    "user": {
        "userId": 1,
        "fullName": "Carolina Ponce",
        "ldapId": "f8cygqn",
        "email": "carolina@blabla.com",
        "uType": {
            "roleId": 1,
            "roleName": "Admin",
            "sections": [
                {
                    "sectionId": 2,
                    "sectionName": "Admin",
                    "components": []
                },
                {
                    "sectionId": 1,
                    "sectionName": "Dashboard",
                    "components": [
                        {
                            "componentId": 2,
                            "componentName": "Provincia",
                            "jsonArray": [
                                {
                                    "ID": "6",
                                    "NAME": "BUENOS AIRES"
                                }
                            ]
                        },
                        {
                            "componentId": 1,
                            "componentName": "Rubros",
                            "jsonArray": [
                                {
                                    "ID": "1",
                                    "NAME": "Automotriz"
                                },
                                {
                                    "ID": "31",
                                    "NAME": "Universidades"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "deleteFlag": "",
        "typeAccess": ""
    }
}
public abstract FetchType fetch

    (Optional) Whether the association should be lazily loaded or must be eagerly
    fetched. The EAGER strategy is a requirement on the persistence provider runtime 
    that the associated entities must be eagerly fetched. The LAZY strategy is a 
    hint to the persistence provider runtime.

Default:
    javax.persistence.FetchType.LAZY
public abstract FetchType fetch

   (Optional) Whether the association should be lazily loaded or must be eagerly 
   fetched. The EAGER strategy is a requirement on the persistence provider runtime
   that the associated entity must be eagerly fetched. The LAZY strategy is a hint 
   to the persistence provider runtime.

Default:
   javax.persistence.FetchType.EAGER