Java JPA StackOverflow——多对多和一对一映射

Java JPA StackOverflow——多对多和一对一映射,java,jpa,spring-data-jpa,hibernate-mapping,entitymanager,Java,Jpa,Spring Data Jpa,Hibernate Mapping,Entitymanager,我正在尝试保存一个JPA实体,该实体与ConsumerDetailstable有许多关系(消费者和产品表)和一对一关系。下面是我的实体 @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class) @Entity p

我正在尝试保存一个JPA实体,该实体与ConsumerDetailstable有许多关系(消费者和产品表)和一对一关系。下面是我的实体

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class)    
    @Entity
    public class Consumer {
        @Id
        @GeneratedValue
        private Long  id;
        private String firstName;
        private String lastName;
        
        @JsonManagedReference
        @OnToMany(mappedBy = "consumer")
        private Set<ConsumerProduct> consumerProducts;
        
        @OneToOne
        private CustomerDetails consumerDetails;
    }  

@Entity
public class Product {
    @Id
    @GeneratedValue
    private Long productId;
    private String productCode;
    
      
    @OneToMany(mappedBy = "product")
    private Set<ConsumerProduct> consumerProducts; 
}



@JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class)   
    @Entity(the join table)
    public class ConsumerProduct {
        @EmbeddedId
        ConsumerProductKey id;
        
       @JsonBackReference
        @ManyToOne
        @MapsId("id")
        @JoinColumn(name = "id")
        private Consumer consumer;
    
        @ManyToOne
        @MapsId("productId")
        @JoinColumn(name = "product_id")
        private Product product;
    }
@Embeddable (forgein keys combined as embeded id)
public class ConsumerProductKey implements Serializable {
    @Column(name="id")
    private Long  id;
                
    @Column(name = "product_id")
    private Long productId;
}

@Enitity (one to one relation table)
public class CustomerDetails {
    @Id
    @GeneratedValue
    private Long consumerDtlId;
    
    @OneToOne
    private Consumer consumer;
    
    private String city;
    private String state;
    private String country;
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonIdentityInfo(生成器=ObjectedGenerators.PropertyGenerator.class)
@实体
公共类消费者{
@身份证
@生成值
私人长id;
私有字符串名;
私有字符串lastName;
@JsonManagedReference
@OnToMany(mappedBy=“消费者”)
专用消费产品;
@奥内托内
私人客户详细信息;
}  
@实体
公共类产品{
@身份证
@生成值
私有长productId;
私有字符串代码;
@OneToMany(mappedBy=“产品”)
专用消费产品;
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonIdentityInfo(生成器=ObjectedGenerators.PropertyGenerator.class)
@实体(联接表)
公共类消费产品{
@嵌入ID
消费者产品密钥id;
@JsonBackReference
@许多酮
@MapsId(“id”)
@JoinColumn(name=“id”)
私人消费者;
@许多酮
@MapsId(“产品ID”)
@JoinColumn(name=“产品标识”)
私人产品;
}
@可嵌入(伪造密钥组合为嵌入id)
公共类ConsumerProductKey实现可序列化{
@列(name=“id”)
私人长id;
@列(name=“product\u id”)
私有长productId;
}
@Enitity(一对一关系表)
公共类客户详细信息{
@身份证
@生成值
私有长consumerDtlId;
@奥内托内
私人消费者;
私人城市;
私有字符串状态;
私人国家;
}
为了保存实体,am刚刚扩展了JPARepository并调用了save方法

public class ConsumerRepository<Consumer> Implements JPARepository<Consumer, Long> {
   @Override
    public Consumer S save(Consumer entity) {
        return save(entity);
    };
}
public类ConsumerRepository实现了JPARepository{
@凌驾
公共消费者储蓄(消费者实体){
返回保存(实体);
};
}
我在save方法中得到java.lang.StackOverflowerError。 我的映射有问题吗

问题:由于这将是保存操作,而且消费者Id尚未生成,我如何分配给以下实体

ConsumerProduct.ConsumerProductKey(一旦消费者表被插入到联接表中,我如何分配它的Id?JPA会处理它吗)

CustomerDetails(一旦消费者表被插入到联接表中,我如何分配它的Id?JPA会处理它吗)


编辑:我已使用JsonManagedReference和JsonBackedReference更新了实体,但仍然面临堆栈溢出错误,这是由于消费者试图访问
消费者产品,而消费者产品试图访问
消费者
实体,最终导致堆栈溢出错误。

您应该分别在
消费者
消费者产品
中使用
@JsonManagedReference
@JsonBackReference
注释。

我尝试过,但仍然存在问题,我已使用JsonManagedReference和JsonBackReference更新了我的问题