2015-12-20 25 views
0

這是我的第一個問題,我是一個總noob。在Java中工作了3個月,所以要溫和。我正在做一個在線註冊跟蹤婚禮和用戶。用戶可以有多個婚禮,反之亦然。它本質上是多對多的關係,但我嘗試了另一條路線:將用戶和婚禮類別與陣列列表相互嵌入。像這樣。將一個帶有空ArrayList的對象添加到我的存儲庫中

package com.theironyard.Entities; 

import javax.persistence.*; 
import java.time.LocalDate; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by macbookair on 12/8/15. 
*/ 

@Entity 
@Table(name = "weddings") 
public class Wedding { 

    public ArrayList <User> guests; 
    // ArrayList here is to store users associated with Wedding. 

    @Id 
    @GeneratedValue 
    @Column(nullable = false) 
    public int id; 

    @Column(nullable = false) 
    public String weddingName; 

    @Column(nullable = false) 
    public String location; 

    @Column(nullable = false) 
    public String date; 


    public Wedding(){ 
    } 

    public Wedding(String date, String location, String weddingName, int id) { 
     this.date = date; 
     this.location = location; 
     this.weddingName = weddingName; 
     this.id = id; 
    } 

    public Wedding(ArrayList guests) { 
     this.guests = guests; 
    } 

    public int getId() { 
     return id; 
    } 

    public ArrayList getGuests() { 
     return guests; 
    } 

    public void setGuests(ArrayList guests) { 
     this.guests = guests; 
    } 

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

    public String getWeddingName() { 
     return weddingName; 
    } 

    public void setWeddingName(String weddingName) { 
     this.weddingName = weddingName; 
    } 

    public String getLocation() { 
     return location; 
    } 

    public void setLocation(String location) { 
     this.location = location; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 
} 

和用戶類

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

    public ArrayList<Wedding>userWeddings; 
    // Array List to store weddings associated with user. 

    @Id 
    @GeneratedValue 
    @Column(nullable = false) 
    public int id; 

    @Column(nullable = false) 
    public String username; 

    @Column(nullable = false) 
    public String phone; 

    @Column(nullable = false) 
    public String email; 

    @Column(nullable = false) 
    public String password; 

    public User() { 
    } 

    public User(ArrayList<Wedding> userWeddings) { 
     this.userWeddings = userWeddings; 
    } 

    public User(int id, String username, String phone, String email, String password) { 
     this.id = id; 
     this.username = username; 
     this.phone = phone; 
     this.email = email; 
     this.password = password; 

    } 

    public ArrayList<Wedding> getUserWeddings() { 
     return userWeddings; 
    } 

    public void setUserWeddings(ArrayList<Wedding> userWeddings) { 
     this.userWeddings = userWeddings; 
    } 

    public int getId() { 
     return id; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public String getPassword() { 
     return password; 
    } 
} 

然後我運行一個「/創建婚禮」的路線,其增加了婚禮和用戶海誓山盟的ArrayList中。我們的目標是,以後大家可以參考一下誰去可以看到婚禮,一個人他們正在與像這樣相關聯的所有婚禮:

@RequestMapping(path = "/create-wedding", method = RequestMethod.POST) 
public Wedding createWedding(@RequestBody Wedding wedding, HttpSession session) throws Exception { 
    weddings.save(wedding); 

    User user = users.findOneByEmail((String) session.getAttribute("email")); 
    if (user == null) { 
     throw new Exception("User does not exist"); 
    } 
    wedding.guests.add(user); 

//將用戶添加到「客人」數組列表婚禮課。

Invite invite = new Invite(); 
    invite.isAdmin= true; 
    invite.wedding= wedding; 
    invite.email = user.email; 
    user.userWeddings.add(wedding); // adding user to List in "User class" 

    return wedding; 
} 

但我得到一個空指針異常,因爲數組列表爲空。我的印象是,如果我沒有明確地說他們是不可空的,那麼默認情況下他們會是。我考慮添加(nullable = true)註釋,但我似乎無法做到這一點,而不必將其作爲數據庫中的列。

基本上,我無法添加用戶/婚禮對象,因爲嵌入式ArrayLists爲空。即使解決這個問題,是否有一種更簡單的方法可以允許多個用戶訪問每個婚禮,反之亦然。

回答

0

您忘記聲明JPA @ManyToMany註釋,它定義了用戶和婚禮實體之間的關係。 (以上兩個字段)

然後,如果您不需要在用戶或婚禮用戶中維護任何婚禮訂單,則可以使用Set接口而不是此列表的雙方的List(ArrayList,在您的示例中)雙向關係。

注:當這個映射定義正確,你永遠不會得到空值包含在從一個管理實體與其他實體的關係集合類型字段(已堅持,不沾邊)

參見this documentation

相關問題