Jan*_*ane 2 java spring hibernate jpa
我想知道我在这里做错了什么,因为我已经完成了加载类型,它不是递归的.但是我收到以下错误.当我尝试从访问对象访问getServerRequest()方法时.
WARN 6511 --- [nio-8080-exec-2] .wsmsDefaultHandlerExceptionResolver:写入HTTP消息失败:org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:无限递归(StackOverflowError)(通过参考链:com. biscoind.domain.ServerRequest [ "访问"] - > org.hibernate.collection.internal.PersistentBag [0] - > com.biscoind.domain.Visit [ "请求"] - > com.biscoind.domain.ServerRequest ["访问"] - > org.hibernate.collection.internal.PersistentBag [0] - > com.biscoind.domain.Visit [" 请求 "] - > com.biscoind.domain.ServerRequest [" 访问"] - > org.hibernate作为. collection.internal.PersistentBag [0] - > com.biscoind.domain.Visit [ "请求"] - > com.biscoind.domain.ServerRequest [ "访问"] - > org.hibernate.collection.internal.PersistentBag [0] - > com.biscoind.domain.Visit [ "请求"] - > com.biscoind.domain.ServerRequest [ "访问"] - > org.hibernate.collection.internal.PersistentBag [0] - > com.biscoind.domain.访问[ "请求"] - > com.biscoind.domain.ServerRequest [ "访问"] - > org.hibernate.collec tion.internal.PersistentBag [0] - > com.biscoind.domain.Visit [ "请求"] - > com.biscoind.domain.ServerRequest [ "访问"] - > org.hibernate.collection.internal.PersistentBag [0] - > com.biscoind.domain.Visit [ "请求"] - > com.biscoind.domain.ServerRequest [ "访问"] - > org.hibernate.collection.internal.PersistentBag [0] - > com.biscoind.domain.访问[ "请求"] - > com.biscoind.domain.ServerRequest [ "访问"] - > org.hibernate.collection.internal.PersistentBag [0] - >
我有以下映射类,
@Entity
@Table(name = "server_request")
public class ServerRequest {
private Long id;
private Date createdDate;
private Date lastUpdatedDate;
private List<Visit> visits = new ArrayList<Visit>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "server_request_id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "request", cascade = CascadeType.ALL)
public List<Visit> getVisits() {
return visits;
}
public void setVisits(List<Visit> visits) {
this.visits = visits;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Date getLastUpdatedDate() {
return lastUpdatedDate;
}
public void setLastUpdatedDate(Date lastUpdatedDate) {
this.lastUpdatedDate = lastUpdatedDate;
}
}
Run Code Online (Sandbox Code Playgroud)
和,
@Entity
@Table(name = "user_visit")
public class Visit implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private ServerRequest request;
private String status;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_visit_id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "server_request_id", referencedColumnName = "server_request_id", nullable = false)
public ServerRequest getRequest() {
return request;
}
public void setRequest(ServerRequest request) {
this.request = request;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您正在使用Jackson并且转换器正在尝试编写JSON(堆栈没有明确说明),请@JsonIdentityInfo在getRequest()方法上添加注释.
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id", scope=ServerRequest.class)
@ManyToOne(fetch = FetchType.EAAGER)
@JoinColumn(name = "server_request_id", referencedColumnName = "server_request_id", nullable = false)
public ServerRequest getRequest() {
return request;
}
Run Code Online (Sandbox Code Playgroud)
这是Jackson在序列化/反序列化期间可以解析对象图中的循环依赖关系的方式.
一些技巧:
FetchType.LAZY.这是默认完成的.FetchType.EAAGER到FetchType.EAGER| 归档时间: |
|
| 查看次数: |
5645 次 |
| 最近记录: |