我使用Spring
和JpaRepository
作为数据访问层。我的@Repository
界面如下:
@Repository
interface EventRepository extends JpaRepository<Event, Long>, JpaSpecificationExecutor<Event> {
.... some custom methods irrelevant to this post ....
}
这是我的简化模型类:
class Event {
Long eventID;
String name;
Date time;
Zone zone;
}
class Zone {
Long zoneId;
String zoneName;
User user;
}
class User {
Long userId;
String username;
}
我使用JpaSpecificationExecutor
来获取特定Event
中time
列表的数据库。我正在使用这种方法,它可以正常工作:
Page<T> findAll(Specification<T> spec, Pageable pageable);
但我想添加另一个条件来仅为特定User
获取条目。
我尝试创建这样的新方法:
Page<Event> findAllByZone_User(User user, Specification<Event> spec, Pageable pageable);
但是当我调用它时,我会进入这一行Exception:
java.util.NoSuchElementException: null
甚至可以创建这样的方法吗?如果没有 - 我该怎么办?
答案 0 :(得分:0)
我创建了一个像你这样的Spring boot project
,这对我有用。你可以查看我的代码.....
<强> Event.java 强>
package com.example.model;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Event {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long eventID;
private String name;
private Date time = new Date();
@OneToOne(cascade = CascadeType.ALL)
private Zone zone;
public Long getEventID() {
return eventID;
}
public void setEventID(Long eventID) {
this.eventID = eventID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public Zone getZone() {
return zone;
}
public void setZone(Zone zone) {
this.zone = zone;
}
}
<强> Zone.java 强>
package com.example.model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Zone {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long zoneId;
private String zoneName;
@OneToOne(cascade = CascadeType.ALL)
private User user;
public Long getZoneId() {
return zoneId;
}
public void setZoneId(Long zoneId) {
this.zoneId = zoneId;
}
public String getZoneName() {
return zoneName;
}
public void setZoneName(String zoneName) {
this.zoneName = zoneName;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
<强> User.java 强>
package com.example.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long userId;
private String username;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
我的存储库是
<强> EventRepository.java 强>
package com.example.model;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EventRepository extends JpaRepository<Event, Long>, JpaSpecificationExecutor<Event>{
Page<Event> findAllByZoneUser(User user, Pageable pageable);
}
<强> UserRepository.java 强>
package com.example.model;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long>{
}
我直接在我的控制器中调用存储库方法,如下所示
<强> UserController.java 强>
package com.example.model;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
EventRepository eventRepository;
@Autowired
UserRepository userRepository;
@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
public Map<String, Object> updateUserDO(@PathVariable Long userId) {
Map<String, Object> map = new HashMap<>();
User user = userRepository.findOne(userId);
PageRequest pageRequest = new PageRequest(0, 10, Sort.Direction.ASC, "eventID");
map.put("user", eventRepository.findAllByZoneUser(user, pageRequest));
return map;
}
}