为什么ResponseEntity在延迟加载活动时调用hibernate

时间:2017-05-12 12:15:16

标签: java hibernate rest

以下是DAO。我得到了第一个UppeningUsers对象。请注意,对于此函数,我不想返回位于UppeningUsers内的peopleWhoBlockedMe集。

但是在不同的功能中,我想返回那些信息。请注意,它们都是LAZY抓取。随着逐出我试图分离对象,但它仍然没有用。

首先,RESTcontroller如下所示。然后DAO代码如下。然后是两个实体描述。

问题是:我看到了

返回新的ResponseEntity(返回,HttpStatus.OK);

只有一个查询是典型的选择。我不希望hibernate去获取特定UppeningUser的UserBlock信息。因为此服务响应不需要它。但即使由于某种原因它是延迟加载 返回新的ResponseEntity(返回,HttpStatus.OK); 调用hibernate。我不知道为什么在restcontroller中它仍然连接到数据库。我试过逐出但没有工作。

json的回应是 { “ID”:7, “peopleWhoBlockedMe”:[{ “blockedId”:7}]}

但是我不希望这个函数让这个人回归。它可以是空的。

请注意,在其他服务中,例如,我将明确要求此人使用WhacklockMe,但仅针对此业务逻辑,我不需要此信息。所以我可以做些什么来防止这种情况,所以每当我真的想给人们打电话时我都能得到它。不是自动的。

  @RestController

  public class TempController {

@Autowired
UppeningUsersService uppeningUsersService;

@RequestMapping(value = "/testing", method = RequestMethod.GET)
public ResponseEntity<UppeningUsers> getPhotos() {
    try {
        UppeningUsers returned = uppeningUsersService.getUsersDetailsPartial();
        return new ResponseEntity<UppeningUsers>(returned, HttpStatus.OK);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}


      }

这部分是DAO。

 @Repository
public class UppeningUsersDAO {

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sf) {
    this.sessionFactory = sf;
}


/**
 * Get Existing user. Return error if there is not.
 * @param incomingUser user who requested access.
 * @return returns the guy information. All information.
 */
@Transactional
public UppeningUsers getUserDetails() throws Exception {
    Session session = this.sessionFactory.getCurrentSession();
    Query query = session.createQuery("from UppeningUsers ");

    UppeningUsers returning = (UppeningUsers) query.list().get(0);

    session.evict(returning);

    return returning;

}



 }

主表就是这个......

    @Entity
    @Table(name = "uppening_users")
    @Proxy(lazy = true)

    public class UppeningUsers {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private
int id;

@OneToMany(mappedBy = "blockedId",cascade =CascadeType.ALL, fetch = FetchType.LAZY)
private Set<UserBlocks> peopleWhoBlockedMe;


public UppeningUsers() {
    super();
}


public int getId() {
    return id;
}

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

    public Set<UserBlocks> getPeopleWhoBlockedMe() {
    return peopleWhoBlockedMe;
}

public void setPeopleWhoBlockedMe(Set<UserBlocks> peopleWhoBlockedMes) {
    this.peopleWhoBlockedMe = peopleWhoBlockedMes;
}


 }

现在这是另一张桌子。

   @Entity
   @Table(name="user_blocks")
   @Proxy(lazy = true)

  public class UserBlocks {

@Id
@Column(name="id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
int id;

@Column(name = "blocked_id",insertable = false,updatable = false)
private int blockedId;


public int getBlockedId() {
    return blockedId;
}

public void setBlockedId(int blockedId) {
    this.blockedId = blockedId;
}

}

更新:2忘了添加服务

     @Service("uppeningUserService")
     public class UppeningUsersService {

@Autowired
UppeningUsersDAO uppeningUsersDAO;



public UppeningUsers getUsersDetailsPartial( ) throws Exception {

    return uppeningUsersDAO.getUserDetails();
}








      }

1 个答案:

答案 0 :(得分:0)

Jens对她的判决是正确的。层方法和编写业务对象可以解决问题。谢谢。

相关问题