下面是DAO。我得到第一個UppeningUsers對象。請注意,在這裏對於這個功能,我不想返回位於UppeningUsers內的peopleWhoBlockedMe集合。爲什麼ResponseEntity在休眠加載時調用休眠
但是在不同的函數中,我想返回這些信息。請注意,它們都是LAZY抓取。隨着evict我試圖分離的對象,但仍然沒有奏效。
首先RESTcontroller在下面。然後DAO代碼如下。接下來是兩個實體描述。
的問題是:我看到,直到
回報新ResponseEntity(返回,HttpStatus.OK);
只有一個查詢是典型的選擇。我不希望休眠去,並採取該特定UppeningUser UserBlock信息。因爲這個服務響應不需要。然而,即使由於某種原因延遲加載 返回新的ResponseEntity(返回,HttpStatus.OK); 調用休眠。我不知道爲什麼在restcontroller它仍然連接到數據庫。我試圖驅逐,但沒有工作。
JSON響應是 { 「ID」:7, 「peopleWhoBlockedMe」:[{ 「blockedId」:7}]}
但我不希望這個函數返回這個peopleWhoBlockedMe。它可以是空的。
請注意,在其他服務,例如,我會明確要求這個peopleWhoBlockedMe,但只是這個業務邏輯,我不需要這些信息。所以,當我真正想打電話給peopleWhoBlockedMe時,我可以做些什麼來防止這種情況發生。不是自動的。
@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();
}
}
在控制器中使用實體類不是一個好主意。閱讀關於3層架構 – Jens
然後它不創建重複的類。爲什麼我不能重用實體中使用的類。 – legend12345
使用一個調試器,看看有多少信息存儲在一個實體中,這對控制器來說是不可見的 – Jens