我在我的项目中使用Hibernate搜索进行全文搜索,并在一个小问题上进行堆叠。 Hibernate构建查询来选择我的依赖“companyLocation”,但我不希望这样。
这是我的模特:
@Getter
@Setter
@Entity
@Table(name = "locations")
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@NotNull(message = "Country code cannot bee null")
@NotBlank(message = "Country code cannot bee blank")
@Size(message = "Country code must contain more than 2 characters", min = 2)
@Column(name = "country_code")
private String countryCode;
@Field
@NotBlank(message = "Country address code cannot bee blank")
@NotNull(message = "Country address code cannot bee null")
@Column
private String address;
@OneToOne
@JoinColumn(name = "company_id")
private Company company;
@Column(name = "secondary_address")
private String secondaryAddress;
@Field
@NotBlank(message = "Country city code cannot bee blank")
@NotNull(message = "Country city code cannot bee null")
@Column
private String city;
@NotBlank(message = "Country state code cannot bee blank")
@NotNull(message = "Country state code cannot bee null")
@Column
private String state;
@NotBlank(message = "Country zip code cannot bee blank")
@NotNull(message = "Country zip code cannot bee null")
@Column
private String zip;
}
和
@Getter
@Setter
@Entity
@Table(name = "companies")
@Indexed
@FullTextFilterDef(name = "companyLocation", impl = Location.class)
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
@NotNull(message = "User id is cannot be null")
@Column(name = "user_id")
private Long userId;
@Field
@NotNull(message = "Company name is cannot be null")
@NotBlank(message = "Company name is cannot be blank")
@Column(name = "company_name")
private String companyName;
@Column(name = "company_phone")
private String companyPhone;
@Column(name = "website_url")
private String websiteUrl;
@Field
@Column(name = "company_description")
private String companyDescription;
@IndexedEmbedded
@OneToOne (mappedBy = "company")
private Location companyLocation;
@Column
private boolean opened = true;
@RestResource
@OneToMany(mappedBy = "company")
private Set<BusinessDay> businessDays;
@IndexedEmbedded
@ManyToMany
@JoinTable(name = "company_categories", joinColumns = @JoinColumn(name = "category_id"),
inverseJoinColumns = @JoinColumn(name = "company_id"))
private Set<BusinessCategory> categories;
@OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
private Set<Rating> ratings;
@OneToMany(mappedBy = "company")
private Set<Review> reviews;
}
全文搜索方法:
private List<T> searchCompany(String args, String[] fields, Pageable pageable) {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(getDomainClass()).get();
Query luceneQuery = queryBuilder.keyword().fuzzy().onFields(fields).matching(args).createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, getDomainClass());
return fullTextQuery
.setFirstResult(pageable.getOffset())
.setMaxResults(pageable.getPageSize())
.getResultList();
}
hibernate创建查询:
Hibernate: select this_.id as id1_2_1_, this_.company_description as company_2_2_1_, this_.company_name as company_3_2_1_, this_.company_phone as company_4_2_1_, this_.opened as opened5_2_1_, this_.user_id as user_id6_2_1_, this_.website_url as website_7_2_1_, location2_.id as id1_5_0_, location2_.address as address2_5_0_, location2_.city as city3_5_0_, location2_.company_id as company_8_5_0_, location2_.country_code as country_4_5_0_, location2_.secondary_address as secondar5_5_0_, location2_.state as state6_5_0_, location2_.zip as zip7_5_0_ from companies this_ left outer join locations location2_ on this_.id=location2_.company_id where (this_.id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?))
查询的第二部分是位置。我不需要那个。
我尝试使用实体图形提示修复它,但是FullTextQuery
已经实现了提示。