简体   繁体   English

我在使用JPA从mssql的关系表中获取数据时遇到问题

[英]I have a problem fetching data from relational table from mssql with JPA

I have a problem fetching data from relational tables with JPA. 我在使用JPA从关系表中获取数据时遇到问题。 I believe that my problem is my use of annotations. 我相信我的问题是我对注释的使用。 Im kinda new to this and would love some guidance. 我对此有点陌生,希望能提供一些指导。

I've had some problem with this before and now that im on vacation I really want to learn Picture entity 我之前对此一直有一些疑问,现在我正在度假,我真的想学习图片实体

@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.

Portfolio entity 投资组合实体

        @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;

In Portfolio Table I have this object 在投资组合表中,我有这个对象

id        address                 description         service
2         Köpenhamnsvägen 33B     some desc           Full-Staging

and in Picture table i Have this object 在图片表中我有这个对象

id        path                 portfolio_id
1         https://bild.se                 2

When I do a simple get request that fires repository.findAll I get this output: 当我执行一个简单的get请求来触发repository.findAll时,我得到以下输出:

获取请求的嵌套对象

It's like 5000 rows of this nested object. 就像这个嵌套对象的5000行一样。 How come? 怎么会?

Best regards! 最好的祝福!

It is because you have a bidirectional mapping, so it comes to a recursion. 这是因为您具有双向映射,所以涉及到递归。 To solve this, just try to add @XmlTransient (JAX-B) or @JsonbTransient/@JsonIgnore (Jackson) to one of the fields. 要解决此问题,只需尝试将@XmlTransient(JAX-B)或@ JsonbTransient / @ JsonIgnore(Jackson)添加到一个字段中。 This will make it 'invisible' in the JSON represantation of your backend/RESTful server. 这将使其在后端/ 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;

or you could do it for the Set: 或者您可以为Set设置:

@OneToMany(mappedBy = "portfolio")
@XmlTransient/@JsonbTransient/@JsonIgnore //again: try them out, I don't know your implementation
private Set<Picture> pictures;

However doing it for both at the same time is not a good idea, since you won't see anything in the request as a result. 但是,同时执行这两个操作并不是一个好主意,因为结果是您将看不到请求中的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM