Java springboot中的一对多关系
我有一个名为data.sql的文件,其中包含sql查询“插入”。 我有一个表用户,它的型号是:Java springboot中的一对多关系,java,sql,json,database,Java,Sql,Json,Database,我有一个名为data.sql的文件,其中包含sql查询“插入”。 我有一个表用户,它的型号是: @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; // other fields @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user") @Column(name = "vaca
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// other fields
@OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy = "user")
@Column(name = "vacations")
private Set<Vacation> vacations = new HashSet<>();
在我的data.sql中,我试图将现有ID插入到休假用户中。
它“传递”抛出了编译器,但在localhost上我只能看到以下内容:
无法写入JSON:无限递归(StackOverflowerError);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:Infinite recursion(StackOverflowError)(通过引用链:com.springproject27.springproject.user.user[“vacations”])
这是H2数据库引擎,我尝试传递的查询是:
插入假期(ID、开始日期、结束日期、类型、原因、用户ID)值
(22、'2012-09-17 18:47:52.69'、'2012-09-20 18:47:52.69'、'无薪假期'、'病假',10)
正如您所看到的,当您试图序列化数据时,由于
用户
实体和假期
实体之间的双向关系,JSON递归会发生这种情况。首选方法是使用@JsonIgnoreProperties
(如果您使用的是Jackson 2.0+版本)在序列化过程中中断递归
注意:另一种打破JSON递归的方法是使用和组合。但是我更喜欢@JsonIgnoreProperties
,因为在序列化过程中没有数据丢失
如果您使用的是Lombok
,请仅使用@Getter&@Setter注释而不是@ToString(或包含@ToString的@Data),因为这将导致相同的递归问题
类用户{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
//其他领域
@OneToMany(级联=级联类型.ALL,
fetch=FetchType.LAZY,
mappedBy=“用户”)
@列(name=“休假”)
@JsonIgnoreProperties(“用户”)
私有设置休假=新HashSet();
}
班级假期{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
@NotNull
@列(name=“开始日期”)
私人LocalDateTime开始休假;
@NotNull
@列(name=“结束日期”)
私人LocalDateTime结束度假日期;
@空空如也
@列(name=“type”)
私人假期;
@空空如也
@列(name=“reason”)
私人假期;
@manytone(cascade={CascadeType.ALL},fetch=FetchType.LAZY)
@JoinColumn(name=“user\u id”,nullable=false)
@JsonIgnoreProperties(“假期”)
私人用户;
}
在您的“JSON”“parse”ing中听起来像是一个“SyntaxError”。这个异常与Spring
或Sql
或数据库
的任何类型都无关。那么,为什么当我在假期中注释query insert时,我可以看到所有其他内容?更新问题您对setter和getter使用的是什么?你在用LOMBOK吗?是的,我在用LOMBOK@DataYeah,这也造成了问题。只能使用龙目的@Getter
和@Setter
。天哪!谢谢兄弟!很高兴我能帮忙。Lombok不是我最喜欢的图书馆,它有魔鬼在里面,但无法避免它使工作变得容易。
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column(name = "begin_date")
private LocalDateTime beginDateOfVacation;
@NotNull
@Column(name = "end_date")
private LocalDateTime endDateOfVacation;
@NotEmpty
@Column(name = "type")
private String typeOfVacation;
@NotEmpty
@Column(name = "reason")
private String reasonOfVacation;
@ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// other fields
@OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy = "user")
@Column(name = "vacations")
@JsonIgnoreProperties("user")
private Set<Vacation> vacations = new HashSet<>();
}
class Vacation{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column(name = "begin_date")
private LocalDateTime beginDateOfVacation;
@NotNull
@Column(name = "end_date")
private LocalDateTime endDateOfVacation;
@NotEmpty
@Column(name = "type")
private String typeOfVacation;
@NotEmpty
@Column(name = "reason")
private String reasonOfVacation;
@ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@JsonIgnoreProperties("vacations")
private User user;
}