Java N+;1具有双向OneTONE映射的Hibernate问题
我正在使用JPARepository和hibernate来表示我的数据库。我有几个实体,我的两个实体有一个双向的OneToOne映射。每当我对双向关系的子类(或获取子类的父类)运行查询时,我都会面临N+1 hibernate问题 我的母公司:Java N+;1具有双向OneTONE映射的Hibernate问题,java,hibernate,jpa,spring-data-jpa,spring-repositories,Java,Hibernate,Jpa,Spring Data Jpa,Spring Repositories,我正在使用JPARepository和hibernate来表示我的数据库。我有几个实体,我的两个实体有一个双向的OneToOne映射。每当我对双向关系的子类(或获取子类的父类)运行查询时,我都会面临N+1 hibernate问题 我的母公司: @Entity @Table(name = "ORGANIZATION") public class Organization { @GeneratedValue(strategy = GenerationType.IDENTITY) @
@Entity
@Table(name = "ORGANIZATION")
public class Organization {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
@Id
private Long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "organization_source_id")
private OrganizationSource source;
//other columns then getters and setters
}
儿童班
@Entity
@Table(name = "ORGANIZATION_SOURCE")
public class OrganizationSource {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
@Id
private Long id;
@OneToOne(mappedBy="source", fetch = FetchType.LAZY)
@Cascade(value=org.hibernate.annotations.CascadeType.SAVE_UPDATE)
private Organization organization;
//other columns then getters and setters
}
然后我使用DAO类来创建查询。我使用了@Query注释
public interface OrganizationSourceDao extends PagingAndSortingRepository<OrganizationSource, Long> {
@Query("SELECT source FROM OrganizationSource source RIGHT JOIN source.organization"
+ "WHERE source.name like %:name% "
// order by part of the query
)
Page<OrganizationSource> findByNameContaining(
@Param("name") String name,
@PageableDefault(size = 10) Pageable pageable);
}
我已经研究过如何使用注释@Fetch(FetchMode.JOIN)
以及谷歌上所有快速解决方案,但似乎无法解决这个问题
任何帮助都将不胜感激。我知道一个窍门。将组织映射从OneToOne更改为ManyToOne。这对我来说一直都很好。这次没有n+1查询 组织来源
@Entity
@Table(name = "ORGANIZATION_SOURCE")
public class **OrganizationSource** {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
@Id
private Long id;
@JsonBackReference
@Cascade(value=org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@ManyToOne(fetch = FetchType.LAZY)
private Organization organization;
private String name;
}
和组织
@Data
@Entity
@Table(name = "ORGANIZATION")
public class Organization {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
@Id
private Long id;
@JsonManagedReference
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "organization_source_id")
private OrganizationSource source;
}
RIGHT JOIN FETCH是在我尝试该操作时获得以下错误的方法:
query指定的联接获取,但获取的关联的所有者不在选择列表[FromElement]中{显式,非集合联接,获取联接,获取非惰性属性,classAlias=organization,role=OrganizationSource.organization,tableName=organization,tableAlias=Organization1,Origination=Organization0,source Organization0,columns={Organization0.id,className=organization}]
你可以使用JOIN-FETCH或LEFT-JOIN-FETCH吗没有任何形式的FETCH对我有效-即使遵循了“适当的”约定,这对我也不起作用。当我尝试使用它时,我得到了一个无效的列名组织\u id
。我添加了一个@JoinColumn(name=“id”,updateable=“false”,insertable=“false)
来指向id(因为这是organization表用作外键的内容。这使得使用组织的id进行搜索。当我放置“organization\u source\u id”时,它会说找不到。当我使用JoinTable时,它工作正常,但organizationSource中的组织值为null。
@Data
@Entity
@Table(name = "ORGANIZATION")
public class Organization {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
@Id
private Long id;
@JsonManagedReference
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "organization_source_id")
private OrganizationSource source;
}