Jpa双向多对一映射即使使用@JsonManagedReference也会陷入无限循环。

huangapple go评论60阅读模式
英文:

Jpa bidirectional many to one mapping going into infinite loop even with @JsonManagedReference

问题

I am using JPA with Oracle databases and need to implement a bidirectional one-to-many and many-to-one mapping for the tables scheme_code_template and multilingual_template_v2 respectively.

MultilingualTemplateV2 Entity

@Entity
@Table(name = "multilingual_template_v2")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class MultilingualTemplateV2 {

    @NotNull()
    String language;

    @NotNull()
    @Nationalized
    String template;

    @Id
    @Column(name="template_code")
    @NotNull()
    String templateCode;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "associated_schemecode")
    @JsonManagedReference
    SchemeCode schemeCode;
}

SchemeCode Entity

@Entity
@Table(name = "scheme_code_templates")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class SchemeCode {

    @Column(name="associated_pushcode")
    Integer associatedPushcode;

    @NotNull
    String description;

    @Id
    @NotNull()
    String schemeCode;

    @OneToMany(mappedBy = "schemeCode", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonBackReference
    List<MultilingualTemplateV2> multilingualTemplates;
}

Database:

Below is scheme_code_templates. It has the scheme_code as a primary key which is used as the foreign key in multilingual_template_v2 table.

Name Null? Type


ASSOCIATED_PUSHCODE NUMBER
SCHEME_CODE NOT NULL VARCHAR2(6)
DESCRIPTION NOT NULL VARCHAR2(200)

Below is multilingual_template_v2. It has the ASSOCIATED_SCHEMECODE as a foreign key referencing the scheme_code in scheme_code_templates. Template_code is the primary key.

Name Null? Type


ASSOCIATED_SCHEMECODE VARCHAR2(6)
TEMPLATE_CODE NOT NULL VARCHAR2(9)
LANGUAGE NOT NULL VARCHAR2(30)
TEMPLATE NOT NULL NCLOB

Used the below query to set the foreign key in multilingual_template_v2

ALTER TABLE multilingual_template_v2
ADD CONSTRAINT fk_scheme_code_templates
  FOREIGN KEY (associated_schemecode)
  REFERENCES scheme_code_templates(scheme_code);

I have created a JpaRepository for scheme_code called SchemeCodeRepository through which I am using findAll(). Currently, I am getting the below error when getMultilingualTemplates() is called.

Error:

at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
...

Any help or suggestion is greatly appreciated.

英文:

I am using JPA with oracle databases and need to implement a bidirectional one to many and many to one mapping for the tables scheme_code_template and multilingual_template_v2 respectively.

MultilingualTemplateV2 Entity

@Entity
@Table(name = &quot;multilingual_template_v2&quot;)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class MultilingualTemplateV2 {

	
	@NotNull()
	String language;
	
	
	@NotNull()
	@Nationalized
	String template;
	
	@Id
	@Column(name=&quot;template_code&quot;)
	@NotNull()
	String templateCode;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = &quot;associated_schemecode&quot;)
	@JsonManagedReference
	SchemeCode schemeCode;
}

SchemeCode Entity

@Entity
@Table(name = &quot;scheme_code_templates&quot;)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class SchemeCode {

	
	@Column(name=&quot;associated_pushcode&quot;)
	Integer associatedPushcode;
	
	@NotNull
	String description;
		
	@Id
	@NotNull()
	String schemeCode;
	
	
	@OneToMany(mappedBy = &quot;schemeCode&quot;, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JsonBackReference
	List&lt;MultilingualTemplateV2&gt; multilingualTemplates;
}

database:

below is scheme_code_templates. It has the scheme_code as primary key which is used as the foreign key in multilingual_template_v2 table

Name                Null?    Type          
------------------- -------- ------------- 
ASSOCIATED_PUSHCODE          NUMBER        
SCHEME_CODE         NOT NULL VARCHAR2(6)   
DESCRIPTION         NOT NULL VARCHAR2(200)

below is multilingual_template_v2 . It has the ASSOCIATED_SCHEMECODE as foreign key referencing the scheme_code in scheme_code_templates. Template_code is the primary key.

Name                  Null?    Type         
--------------------- -------- ------------ 
ASSOCIATED_SCHEMECODE          VARCHAR2(6)  
TEMPLATE_CODE         NOT NULL VARCHAR2(9)  
LANGUAGE              NOT NULL VARCHAR2(30) 
TEMPLATE              NOT NULL NCLOB 

Used the below query to set foreign key in multilingual_template_v2

ALTER TABLE multilingual_template_v2
ADD CONSTRAINT fk_scheme_code_templates
  FOREIGN KEY (associated_schemecode)
  REFERENCES scheme_code_templates(scheme_code);

I have created a JpaRepository for scheme_code called SchemeCodeRepository through which I am using findAll(). Currently I am getting below error when getMultilingualTemplates() is called.

for (SchemeCode schemeCode : schemeRepo.findAll()) {
			System.out.println(schemeCode.getMultilingualTemplates());
		}

Error

at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.SchemeCode.toString(SchemeCode.java:34) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.MultilingualTemplateV2.toString(MultilingualTemplateV2.java:30) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[?:?]
	at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.SchemeCode.toString(SchemeCode.java:34) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.MultilingualTemplateV2.toString(MultilingualTemplateV2.java:30) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[?:?]
	at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.SchemeCode.toString(SchemeCode.java:34) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.MultilingualTemplateV2.toString(MultilingualTemplateV2.java:30) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[?:?]
	at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.SchemeCode.toString(SchemeCode.java:34) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.MultilingualTemplateV2.toString(MultilingualTemplateV2.java:30) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[?:?]
	at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.SchemeCode.toString(SchemeCode.java:34) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.MultilingualTemplateV2.toString(MultilingualTemplateV2.java:30) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[?:?]
	at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.SchemeCode.toString(SchemeCode.java:34) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.MultilingualTemplateV2.toString(MultilingualTemplateV2.java:30) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at java.util.AbstractCollection.toString(AbstractCollection.java:457) ~[?:?]
	at org.hibernate.collection.spi.PersistentBag.toString(PersistentBag.java:585) ~[hibernate-core-6.1.7.Final.jar:6.1.7.Final]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]
	at java.lang.StringBuilder.append(StringBuilder.java:173) ~[?:?]
	at com.dcb.smsbatch.dataLayer.entities.SchemeCode.toString(SchemeCode.java:34) ~[classes/:?]
	at java.lang.String.valueOf(String.java:4218) ~[?:?]

Any help or suggestion is greatly appreciated.

答案1

得分: 2

递归是由 @ToString lombok 注解和双向引用引起的。生成的 toString 方法不知道任何 @JsonManagedReference(用于 Jackson 序列化)。没有与 @JsonBackReference 等效的东西,但你可以用 @ToString.Exclude 注解该字段。

@OneToMany(mappedBy = "schemeCode", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonBackReference
@ToString.Exclude
List<MultilingualTemplateV2> multilingualTemplates;
英文:

The recursion is caused by @ToString lombok annotation and the two way reference. The generated toString methods are unaware of any @JsonManagedReference (which is for jackson serialization)

There is no such equivalent as @JsonBackReference, but you could annotate the field with @ToString.Exclude

@OneToMany(mappedBy = &quot;schemeCode&quot;, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonBackReference
@ToString.Exclude
List&lt;MultilingualTemplateV2&gt; multilingualTemplates;

huangapple
  • 本文由 发表于 2023年5月22日 19:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定