外键未显示在GET请求中

时间:2020-05-01 12:43:04

标签: java spring spring-boot jpa

我正在执行GET请求“ / api / admin / tickets”。我没有得到userId作为响应。我已经检查了数据库,并且那里存在userId。在实体“票证”中,userId应该充当外键。我不知道为什么我看不到外键。我正在尝试在用户和票证之间建立一对多的关系。

TicketController.java

return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "posts_post" already exists

User.java

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = Constants.ORIGIN)
public class TicketController {

    @Autowired
    private TicketRepository ticketRepository;

    @GetMapping("/admin/tickets")
    public Page<Ticket> getAllTickets(Pageable pageable) throws ResourceNotFoundException{
        return ticketRepository.findAll(pageable);
    }
}

Ticket.java

@Entity
public class User extends AuditModel {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;


    @Column(nullable = false)
    private String businessUnit;

    @Column(nullable = false)
    private String title;

    public User() {
    }

    // constructor with fields
    // getters and setters

}

TicketRepository.java

@Entity
public class Ticket extends AuditModel {
    @Id
    @Column(name = "ticketId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "userId", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonIgnore
    private User user;

    public Ticket() {
        super();
    }

    public Ticket(int id) {
        super();
        this.id = id;
    }

    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

1 个答案:

答案 0 :(得分:1)

您用User注释了Ticket类中的@JsonIgnore字段,这可以防止User数据格式被包含在GET响应中。

第二个问题可能是您使用User延迟加载了FetchType.LAZY字段。这也可能会阻止数据包含在GET响应中。