因此,我正在尝试为项目构建类似Twitter的外观。但是,当我尝试从数据库中提取某些数据时,出现了stackoverflow错误。我想出了这个错误,仅当我尝试使用其各自的kweet获取用户(或用户列表)时才会发生。获取以下用户可以正常工作。我的代码如下
@Entity
@Table
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "User.getByUsername", query = "SELECT u FROM User u WHERE
u.username = :username")
,
@NamedQuery(name = "User.getByEmail", query = "SELECT u FROM User u WHERE
u.email = :email"),
@NamedQuery(name = "User.getLogin", query = "SELECT u FROM User u WHERE
u.password = :password AND u.email = :email")
})
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Role Role;
@Column
private String username;
@Column
private String password;
@Column
private String email;
private String location;
private String weburl;
@Column(length = 160)
private String bio;
@ManyToMany
private List<User> followedUsers;
@ManyToMany
private List<User> followingUsers;
@OneToMany(mappedBy = "owner", cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.LAZY)
private List<Kweet> kweets;
public User() {
}
public void addFollowingUser(User user){
followedUsers.add(user);
}
public void removeFollowingUser(User user){
followedUsers.remove(user);
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setFollowedUsers(List<User> followedUsers) {
this.followedUsers = followedUsers;
}
public void setFollowingUsers(List<User> followingUsers) {
this.followingUsers = followingUsers;
}
public void setKweets(List<Kweet> kweets) {
this.kweets = kweets;
}
public void setEmail(String email) {
this.email = email;
}
public void setLocation(String location) {
this.location = location;
}
public void setWeburl(String weburl) {
this.weburl = weburl;
}
public void setBio(String bio) {
this.bio = bio;
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
@XmlTransient
public List<User> getFollowedUsers() {
return followedUsers;
}
@XmlTransient
public List<User> getFollowingUsers() {
return followingUsers;
}
@XmlTransient
public List<Kweet> getKweets() {
return kweets;
}
public Role getRole() {
return Role;
}
public String getEmail() {
return email;
}
public String getLocation() {
return location;
}
public String getWeburl() {
return weburl;
}
public String getBio() {
return bio;
}
public void addKweet(Kweet k) {
kweets.add(k);
}
public void setRole(Role role) {
this.Role = role;
}
对于用户类(应该在其中嵌套kweet
@Entity
@Table
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Kweet.findByText",query = "SELECT k FROM Kweet k WHERE
k.text = :text"),
@NamedQuery(name = "Kweet.findByUId",query = "SELECT k FROM Kweet k WHERE
k.owner.id =:id")
})
public class Kweet implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(optional = false, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private User owner;
private String username;
@OneToMany
private List<User> rekweets;
private String text;
public Kweet() {
}
public Long getId() {
return id;
}
public User getOwner() {
return owner;
}
public String getUsername() {
return username;
}
@XmlTransient
public List<User> getRekweets() {
return rekweets;
}
public String getText() {
return text;
}
public void setId(Long id) {
this.id = id;
}
public void setOwner(User owner) {
this.owner = owner;
}
public void setUsername(String username) {
this.username = username;
}
public void setRekweets(List<User> rekweets) {
this.rekweets = rekweets;
}
public void setText(String text) {
this.text = text;
}
}
对于只能有一个用户的kweet类
我的问题是我在做错什么(为什么我没有得到属于用户的提示)以及如何解决这个问题