我正在尝试将Set放在另一个Object中。 例如:
我正在返回一个论坛列表,但在那些论坛中它包含一些线程。我在调用函数时如何获取这些线程:
@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();
}
我想知道两件事:
论坛课程:
@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 :(得分:1)
有人可以解释我为什么没有得到任何线程吗?
在您的实际代码中:
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);
您没有在threads
列表中添加任何内容,只是创建Thread
对象而不将其添加到列表中,然后返回空List
。
在将创建的Thread
对象设置为Forum
对象之前,应将其添加到列表中。
threads.add(thread); //add the created thred to your List
forum.setThreads(threads);
forum.setId(1);
修改强>
回答你的第二个问题:
获取所有主题但仅包含ID号。
要仅获得ID
中Threads
的{{1}},您应该创建一个具有List
属性的自定义DTO
对象,并且{ {1}}只保留Forum
类的List<Integer>
个,并使用自定义代码填充此列表。