2017-05-26 2 views
-2

私はセットを別のオブジェクトの中に入れようとしています。 例:Hibernate〜RESTfulはオブジェクトのセットを返します

私はフォーラムのリストを返信していますが、フォーラムにはスレッドが含まれています。私は、関数を呼び出すとき、私はそれらのスレッドを取得するにはどうすればよい:

@GET 
@Produces({MediaType.APPLICATION_JSON}) 
public Response findAll() { 
    List<Forum> forums = new ArrayList<>(); 

    Forum forum = new Forum(); 
    WebAccount account = new WebAccount("", "", "", Gender.MALE, Long.MAX_VALUE); 
    forum.setAccount(account); 

    List<ForumThread> threads = new ArrayList<>(); 

    ForumThread thread = new ForumThread(); 
    thread.setAccount(account); 
    thread.setId(1); 
    thread.setName("sweg"); 
    thread.setDescription("LOLOL"); 

    forum.setThreads(threads); 
    forum.setId(1); 

    Forum forum1 = new Forum(); 
    WebAccount account1 = new WebAccount("", "", "", Gender.MALE, Long.MAX_VALUE); 
    forum1.setAccount(account1); 
    forum1.setId(2); 

    forums.add(forum); 
    forums.add(forum1); 
    GenericEntity<List<Forum>> entity = new GenericEntity<List<Forum>>(forums) {}; 
    return Response.ok(entity).build(); 
} 

私は二つのことを知っていただきたいと思います:

  1. がすべてのスレッドだけでID番号を取得します。
  2. すべての単一スレッドのスレッド情報全体を取得します。

フォーラムクラス:私はすべてのスレッドを取得していない午前理由

@Entity 
@NamedQueries({}) 
@XmlRootElement 
@Table(name="Website_Forum") 
public class Forum implements Serializable { 

@Id 
@Column(unique = true, nullable = false, length = 16) 
@TableGenerator(name = "ForumSEQ", allocationSize = 1, initialValue = 1) 
@GeneratedValue(strategy = GenerationType.TABLE, generator = "ForumSEQ") 
private Integer id; 

@ManyToOne(fetch = FetchType.LAZY) 
@JoinColumn(name = "webaccount_id") 
private WebAccount account; 

@Column(unique = true, nullable = false, length = 64) 
private String name; 

@Column(unique = false, nullable = false, length = 1024) 
private String description; 

@OneToMany(cascade = CascadeType.ALL) 
@LazyCollection(LazyCollectionOption.FALSE) 
@JoinTable(name = "Website_ForumThreads", 
     joinColumns = @JoinColumn(name = "forum_id", referencedColumnName = "id"), 
     inverseJoinColumns = @JoinColumn(name = "thread_id", referencedColumnName = "id")) 
private List<ForumThread> threads; 

public Forum() { 
} 

public Integer getId() { 
    return id; 
} 

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

public WebAccount getAccount() { 
    return account; 
} 

public void setAccount(WebAccount account) { 
    this.account = account; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public List<ForumThread> getThreads() { 
    return threads; 
} 

public void setThreads(List<ForumThread> threads) { 
    this.threads = threads; 
} 
} 

誰かが私に説明してもらえますか? 私は関数を呼び出すとき、これは私のJSON出力です:

[ 
    { 
    "account": { 
     "dateOfBirth": 9223372036854775807, 
     "email": "", 
     "gender": "MALE", 
     "helpId": "9c5ee012-4c35-4c6c-be0f-aa8b383642a5", 
     "helpType": "ACCOUNT_CREATE", 
     "information": "A new account!", 
     "isBanned": false, 
     "password": "", 
     "permissionGroup": "REGULAR", 
     "profilePicture": "/Media/ProfilePictures/default.png", 
     "username": "" 
    }, 
    "id": 1, 
    "threads": [] 
    }, 
    { 
    "account": { 
     "dateOfBirth": 9223372036854775807, 
     "email": "", 
     "gender": "MALE", 
     "helpId": "590b8ccb-5c96-4044-8e58-424f5d5287c6", 
     "helpType": "ACCOUNT_CREATE", 
     "information": "A new account!", 
     "isBanned": false, 
     "password": "", 
     "permissionGroup": "REGULAR", 
     "profilePicture": "/Media/ProfilePictures/default.png", 
     "username": "" 
    }, 
    "id": 2 
    } 
] 
+0

クラスを表示してください。 – Jens

+0

フォーラムクラス全体を見ることができます。 – runefist

+1

'threads.add(thread)'の呼び出しが表示されないので、リストは空です。 –

答えて

1

私は任意のスレッドを取得していない午前、なぜ誰かが私に説明してもらえますか?あなたの実際のコードで

:あなたはあなたのthreadsリストには何も追加されていません

List<ForumThread> threads = new ArrayList<>(); 

ForumThread thread = new ForumThread(); 
thread.setAccount(account); 
thread.setId(1); 
thread.setName("sweg"); 
thread.setDescription("LOLOL"); 

forum.setThreads(threads); 
forum.setId(1); 

、あなただけのリストに追加せずにThreadオブジェクトを作成し、この空のListを返しています。

Forumオブジェクトに設定する前に、作成したThreadオブジェクトをリストに追加する必要があります。

threads.add(thread); //add the created thred to your List 
forum.setThreads(threads); 
forum.setId(1); 

編集:

あなたの2つ目の質問に答えるために:

をすべてのスレッドだけでID番号を取得します。

そして、あなたはThreadクラスの唯一のID Sを保持し、かつにカスタムコードを使用するForum属性を持つカスタマイズされたDTOオブジェクトを作成し、List<Integer>すべきListThreadsの唯一ID Sを取得しますこのリストを記入してください。

+0

あなたはすでにそれを解決しました、ありがとう、たくさん!多分あなたは解決策を持っていますか?フォーラムスレッドIDだけを返しますか? – runefist

+0

私はあなたのやりたいことで自分の答えを編集しました。 –

関連する問題