2017-08-10 63 views
0

我使用SpringJpaRepository作爲數據訪問層。我有我的@Repository界面如下:Spring JpaSpecificationExecutor新的查詢方法NoSuchElementException

@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爲特定timeEvent的名單獲取數據庫。我使用這個方法,它正常工作:

Page<T> findAll(Specification<T> spec, Pageable pageable); 

但我想添加其他條件以便獲取只對特定User條目。

我試圖建立這樣的新方法:

Page<Event> findAllByZone_User(User user, Specification<Event> spec, Pageable pageable); 

但是,當我把它稱爲我得到這一行例外:

java.util.NoSuchElementException: null 

它甚至有可能創造這樣的方法?如果不是 - 我該如何繼續?

+0

你可以嘗試findByZoneUser(用戶用戶) – Barath

+0

@barath但什麼'Specification'和'Pageable'? – user3626048

+0

嘗試findByZoneUser(用戶用戶,規格規格,可分頁分頁) – CIPHER007

回答

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; 
    } 
} 
+0

您還可以在我的倉庫和控制器'頁 findAllByZoneUserUserId(龍用戶id,可分頁較小的變動後得到相同的結果);並在我的控制器中刪除User User = userRepository.findOne(userId);並將eventRepository.findAllByZoneUser(user,pageRequest)更改爲'eventRepository.findAllByZoneUserUserId(userId,pageRequest)' – CIPHER007

+0

不需要創建'UserRepository.java' – CIPHER007

+0

謝謝你的例子,它可以工作,但是當我添加'Specification '作爲方法參數時,它停止工作。我通過在實現'Specification <>'接口的類中添加用戶過濾來解決問題 – user3626048

相關問題