Stack java 11 springboot 2.7我不能,创建了一个有多重关系的实体模式,我有几个错误,我不能摆脱它我不,最好的错误是什么?
用户实体应仅将此数据保存到备份
Address实体应仅保存此数据,并级联有关保存的委托,并且引用ID永远不会更改。
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(...)
@EntityListeners(AuditingEntityListener.class)
@Builder
public class AdressMSSEntity {
@Id
@Column(name = "...")
private String adress;
@ManyToOne(optional = false)
@JoinColumn(name = "...")
private UserEntity useEntity;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "adress", cascade = CascadeType.ALL, orphanRemoval = true)
private List<DelegueEntity> delegueEntities;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "adressDelegue")
private List<DelegueEntity> delegationRecues;
}
@Data
@AllArgsConstructor
@Entity
@Table(name = "...")
@EntityListeners(AuditingEntityListener.class)
@Builder
@NoArgsConstructor
public class DelegueEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "...")
private Integer id;
@ManyToOne
@JoinColumn(name = "...")
private AdressEntity adress;
@ManyToOne
@JoinColumn(name = "...")
private AdressEntity adressDelegue;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(...)
@EntityListeners(AuditingEntityListener.class)
@Builder
public class UserEntity {
@Id
@Column(name = "...")
private String idTech;
@OneToMany(fetch = FetchType.EAGER)
@Cascade({PERSIST, MERGE, REMOVE, REFRESH, DETACH})
private List<AdressEntity> listAdressEntity;
}
@Repository
@Transactional
@PersistenceContext
public interface IAdressRepositoryJpa extends JpaRepository<AdressEntity, String> {
Page<AdressEntity> findByAdressContaining(String adress, Pageable pageable);
Page<AdressEntity> findByUserEntityIdTechContaining(String idTech, Pageable pageable);
Page<AdressEntity> findByUserEntityContainingAndAdressContaining(String adress, String idTech,
Pageable pageable);
@Query("select distinct a from AdressEntity a left join DelegueEntity d.adress where a.adress "
+ "= :adress.adress")
List<AdressEntity> findAllDelegueEntitiesAdressByAdressEntity(
@Param("adress") AdressEntity adress);
@Query("select distinct a from AdressEntity a left join DelegueEntity d.adressDelegue = :adress.adress")
List<AdressEntity> findAllDelegationsRecuesAdressDelegueByAdressEntity(
@Param("adress") AdressEntity adress);
@Query("select distinct a from AdressEntity a left join UserEntity i.idTech = :adress.adress")
List<AdressEntity> findAllUserEntityIdTechByAdressEntity(
@Param("adress") AdressEntity adress);
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlserverEntityManager' defined in class path resource [.../PersistenceSqlserverAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [....UserEntity.listAdressEntity, ....AdressEntity.delegationRecues]
1条答案
按热度按时间ryevplcw1#
要在两个实体之间建立双向关系,你需要使用
@ManyToMany
注解。这里有一篇很好的文章解释了如何做到这一点:Many-To-Many Relationship in JPA