![](/img/trans.png)
[英]How can I update a field of relational table with a data from a related table
[英]I have a problem fetching data from relational table from mssql with JPA
我在使用JPA从关系表中获取数据时遇到问题。 我相信我的问题是我对注释的使用。 我对此有点陌生,希望能提供一些指导。
我之前对此一直有一些疑问,现在我正在度假,我真的想学习图片实体
@Entity
@Table(name = "Picture")
public class Picture {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
private int id;
@Column(name = "path")
@NotNull
private String path;
@ManyToOne
@JoinColumn(name = "portfolio_id", nullable = false)
private Portfolio portfolio;
and getters, setters.
投资组合实体
@Entity
@Table(name = "Portfolio")
public class Portfolio {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
@NotNull
private int id;
@Column(name = "Address")
@NotNull
private String address;
@Column(name = "Description")
@NotNull
private String description;
@Column(name = "Service")
@NotNull
private String service;
@OneToMany(mappedBy = "portfolio")
private Set<Picture> pictures;
在投资组合表中,我有这个对象
id address description service
2 Köpenhamnsvägen 33B some desc Full-Staging
在图片表中我有这个对象
id path portfolio_id
1 https://bild.se 2
当我执行一个简单的get请求来触发repository.findAll时,我得到以下输出:
就像这个嵌套对象的5000行一样。 怎么会?
最好的祝福!
这是因为您具有双向映射,所以涉及到递归。 要解决此问题,只需尝试将@XmlTransient(JAX-B)或@ JsonbTransient / @ JsonIgnore(Jackson)添加到一个字段中。 这将使其在后端/ RESTful服务器的JSON表示中“不可见”。
@ManyToOne
@JoinColumn(name = "portfolio_id", nullable = false)
@XmlTransient/@JsonbTransient/@JsonIgnore (try all 3 annotations, since I dont know what implemantation of JAX-RS you are using
private Portfolio portfolio;
或者您可以为Set设置:
@OneToMany(mappedBy = "portfolio")
@XmlTransient/@JsonbTransient/@JsonIgnore //again: try them out, I don't know your implementation
private Set<Picture> pictures;
但是,同时执行这两个操作并不是一个好主意,因为结果是您将看不到请求中的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.