2017-06-11 70 views
0

我有兩個表,即用戶和事件。用戶表將在新用戶註冊時填寫。稍後相同的用戶可以創建日曆事件。所以事件表將被填充,並且users_events將基於用戶保持事件的映射。 我想找到所有基於登錄userId的事件。所以這裏是查詢,它應該基於它返回數據。 從其中包含eventid的事件中選擇*(從user_event中選擇eventId,其中id_user = x)。這是我的用戶和事件實體類。如何使用CrudRepository在springboot中基於​​inverseColumn數據檢索數據?

User.java

@Entity 
    @Table(name = "users") 
    public class User { 

@Id 
@GenericGenerator(name = "generator", strategy = "increment") 
@GeneratedValue(generator = "generator") 
@Column(name = "id", nullable = false) 
private Long id; 

@Column(name = "first_name", nullable = false) 
private String firstName; 

@Column(name = "family_name", nullable = false) 
private String familyName; 

@Column(name = "e_mail", nullable = false) 
private String email; 

@Column(name = "phone", nullable = false) 
private String phone; 

@Column(name = "language", nullable = false) 
private String language; 

@Column(name = "id_picture") 
private String pictureId; 

@Column(name = "login", nullable = false) 
private String login; 

@Column(name = "password", nullable = false) 
private String password; 

@Column(name = "birth_date") 
private Date birthDate; 

@Column(name = "enabled") 
private Boolean enabled; 
    //getter and setter 

Event.java

@Entity 
    @Table(name = "events") 
    public class Event { 
@Id 
@GenericGenerator(name = "generator", strategy = "increment") 
@GeneratedValue(generator = "generator") 
@Column(name = "eventId", nullable = false) 
private Long eventId; 

@Column(name = "title", nullable = false) 
private String title; 

@Column(name = "description", nullable = true) 
private String description; 

@Column(name = "startAt", nullable = false) 
@Temporal(TemporalType.TIMESTAMP) 
private Date startAt; 

@Column(name = "endAt", nullable = true) 
@Temporal(TemporalType.TIMESTAMP) 
private Date endAt; 

@Column(name = "isFullDay", nullable = false) 
private Boolean isFullDay; 

@ManyToMany(fetch = FetchType.EAGER) 
@JoinTable(name = "users_event", joinColumns = { @JoinColumn(name = "id_event", referencedColumnName = "eventId") }, inverseJoinColumns = { @JoinColumn(name = "id_user", table = "users", referencedColumnName = "id") }) 
private Set<User> user = new HashSet<User>(); 
/getter and setter 

EventRepo.java

public interface EventRepo extends CrudRepository<Event, Long> { 
Event findByUser(Set<User> user); 
    } 

我想實現的東西,它可以給我這個查詢的輸出。 select * from event where eventid in(select eventId from users_event where id_user = x) 這裏是我的implementation.any輸入嗎?

 @RequestMapping(value = "/events", method = RequestMethod.GET) 
public @ResponseBody List<Event> getEvents() { 
    logger.debug("get event list"); 
    User x=new User(); 
    x.setId(1); 
    Set<User> user= new HashSet(); 
    user.add(x); 
    return (List<Event>) eventRepo.findByUser(user); 
} 

回答

0

只需添加一個下面的方法您EventRepo

List<Event> findAllByUserId(Long userId); 

並修改您的控制器是這樣的:

@RequestMapping(value = "/events", method = RequestMethod.GET) 
public List<Event> getEvents() { 
    return eventRepo.findAllByUserId(1L); 
} 
+0

它的工作!是否有任何規則來定義方法'findAllByUserId'?還有什麼其他領域,我可以使用從用戶表進行查詢? – vivek

+0

是的,規則是在這裏定義的(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation)。你應該可以使用幾乎任何領域。 –

+0

還有什麼辦法,同時保存事件數據,我可以通過UserId而不是用戶表的所有現有列數據?目前,我做這樣的: 標題:標題, 說明:說明, startAt:開始時間, ENDAT:結束時間, isFullDay:假的, 用戶:[{ 「ID」:1, 「名字」: 「史蒂夫」 ,「familyName」:「JOBS」,「email」:「[email protected]」,「phone」:「0033 1 23 45 67 89」,「language」:「en」,「pictureId」 「登錄」: 「史蒂夫」, 「密碼」: 「史蒂夫」, 「burthDate」:空, 「已啓用」:真實的, 「權威」:[{ 「ID」:1, 「名」: 「管理員」},{ 「id」:2,「name」:「technical user」},{「id」:3,「name」:「user」}]}] – vivek

相關問題