如何编写一个hibernate搜索查询

时间:2013-12-24 06:58:07

标签: hibernate hibernate-search

如何编写hibernate搜索连接查询。

@Entity
@Indexed
@Table(name = "raas_entity_information")
public class EntityModel implements Serializable {

private static final long serialVersionUID = 2605343079798123587L;

private String requestor;
private Integer entity_id;
private String entity_type;
private String entity_status;
private String account_type;
private String short_name;
private String first_name;
private String middle_name;
private String last_name;
private String managed_country;
private String govt_ownership;
private String govt_percentage;
private String govt_name;
private String legal_form;
private String country_of_residence;
private String country_of_citzenship;
private Date date_of_birth;
private String country_of_incorporation;
private Date date_of_incorporation;
private String created_by;
private Timestamp created_on;
private String updated_by;
private Timestamp updated_on;
private Set<EntityAddress> entityAddresses = new HashSet<EntityAddress>(0);
private Set<EntityDetails> entityDetails = new HashSet<EntityDetails>(0);

public EntityModel(){

}

public EntityModel(String requestor, String entity_type,
        String entity_status, String account_type, String short_name,
        String first_name, String middle_name, String last_name,
        String managed_country,
        String govt_ownership, String govt_percentage, String govt_name,
        String legal_form, String country_of_residence,
        String country_of_citzenship, Date date_of_birth,
        String country_of_incorporation, Date date_of_incorporation,
        String created_by, Timestamp created_on, String updated_by,
        Timestamp updated_on, Set<EntityAddress> entityAddresses,
        Set<EntityDetails> entityDetails) {
    super();
    this.requestor = requestor;
    this.entity_type = entity_type;
    this.entity_status = entity_status;
    this.account_type = account_type;
    this.short_name = short_name;
    this.first_name = first_name;
    this.middle_name = middle_name;
    this.last_name = last_name;
    this.managed_country = managed_country;
    this.govt_ownership = govt_ownership;
    this.govt_percentage = govt_percentage;
    this.govt_name = govt_name;
    this.legal_form = legal_form;
    this.country_of_residence = country_of_residence;
    this.country_of_citzenship = country_of_citzenship;
    this.date_of_birth = date_of_birth;
    this.country_of_incorporation = country_of_incorporation;
    this.date_of_incorporation = date_of_incorporation;
    this.created_by = created_by;
    this.created_on = created_on;
    this.updated_by = updated_by;
    this.updated_on = updated_on;
    this.entityAddresses = entityAddresses;
    this.entityDetails = entityDetails;
}

@Column(name = "requestor", unique = true, nullable = true, length = 10)
public String getRequestor() {
    return requestor;
}

public void setRequestor(String requestor) {
    this.requestor = requestor;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "entity_id", unique = true, nullable = false)
public Integer getEntity_id() {
    return entity_id;
}

public void setEntity_id(Integer entity_id) {
    this.entity_id = entity_id;
}

@Column(name = "entity_type", unique = false, nullable = true, length = 10)
public String getEntity_type() {
    return entity_type;
}

public void setEntity_type(String entity_type) {
    this.entity_type = entity_type;
}

@Column(name = "entity_status", unique = false, nullable = true, length = 10)
public String getEntity_status() {
    return entity_status;
}

public void setEntity_status(String entity_status) {
    this.entity_status = entity_status;
}

@Column(name = "account_type", unique = false, nullable = true, length = 10)
public String getAccount_type() {
    return account_type;
}

public void setAccount_type(String account_type) {
    this.account_type = account_type;
}

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
@Column(name = "short_name", unique = false, nullable = true, length = 10)
public String getShort_name() {
    return short_name;
}

public void setShort_name(String short_name) {
    this.short_name = short_name;
}

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
@Column(name = "first_name", unique = false, nullable = true, length = 10)
public String getFirst_name() {
    return first_name;
}

public void setFirst_name(String first_name) {
    this.first_name = first_name;
}

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
@Column(name = "middle_name", unique = false, nullable = true, length = 10)
public String getMiddle_name() {
    return middle_name;
}

public void setMiddle_name(String middle_name) {
    this.middle_name = middle_name;
}

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
@Column(name = "last_name", unique = false, nullable = true, length = 10)
public String getLast_name() {
    return last_name;
}

public void setLast_name(String last_name) {
    this.last_name = last_name;
}

@Column(name = "managed_country", unique = false, nullable = true, length = 10)
public String getManaged_country() {
    return managed_country;
}

public void setManaged_country(String managed_country) {
    this.managed_country = managed_country;
}

@Column(name = "govt_ownership", unique = false, nullable = true, length = 10)
public String getGovt_ownership() {
    return govt_ownership;
}

public void setGovt_ownership(String govt_ownership) {
    this.govt_ownership = govt_ownership;
}

@Column(name = "govt_percentage", unique = false, nullable = true, length = 10)
public String getGovt_percentage() {
    return govt_percentage;
}

public void setGovt_percentage(String govt_percentage) {
    this.govt_percentage = govt_percentage;
}

@Column(name = "govt_name", unique = false, nullable = true, length = 10)
public String getGovt_name() {
    return govt_name;
}

public void setGovt_name(String govt_name) {
    this.govt_name = govt_name;
}

@Column(name = "legal_form", unique = false, nullable = true, length = 10)
public String getLegal_form() {
    return legal_form;
}

public void setLegal_form(String legal_form) {
    this.legal_form = legal_form;
}

@Column(name = "country_of_residence", unique = false, nullable = true, length =10)
public String getCountry_of_residence() {
    return country_of_residence;
}

public void setCountry_of_residence(String country_of_residence) {
    this.country_of_residence = country_of_residence;
}

@Column(name = "country_of_citzenship", unique = false, nullable = true, length=10)
public String getCountry_of_citzenship() {
    return country_of_citzenship;
}

public void setCountry_of_citzenship(String country_of_citzenship) {
    this.country_of_citzenship = country_of_citzenship;
}

@Column(name = "date_of_birth", unique = false, nullable = true, length = 10)
public Date getDate_of_birth() {
    return date_of_birth;
}

public void setDate_of_birth(Date date_of_birth) {
    this.date_of_birth = date_of_birth;
}

@Column(name = "country_of_incorporation", unique=false, nullable=true, length=10)
public String getCountry_of_incorporation() {
    return country_of_incorporation;
}

public void setCountry_of_incorporation(String country_of_incorporation) {
    this.country_of_incorporation = country_of_incorporation;
}

@Column(name = "date_of_incorporation", unique = false, nullable = true, length=10)
public Date getDate_of_incorporation() {
    return date_of_incorporation;
}

public void setDate_of_incorporation(Date date_of_incorporation) {
    this.date_of_incorporation = date_of_incorporation;
}

@Column(name = "created_by", unique = false, nullable = true, length = 10)
public String getCreated_by() {
    return created_by;
}

public void setCreated_by(String created_by) {
    this.created_by = created_by;
}

@Column(name = "created_on", unique = false, nullable = true, length = 10)
public Timestamp getCreated_on() {
    return created_on;
}

public void setCreated_on(Timestamp created_on) {
    this.created_on = created_on;
}

@Column(name = "updated_by", unique = false, nullable = true, length = 10)
public String getUpdated_by() {
    return updated_by;
}

public void setUpdated_by(String updated_by) {
    this.updated_by = updated_by;
}

@Column(name = "updated_on", unique = false, nullable = true, length = 10)
public Timestamp getUpdated_on() {
    return updated_on;
}

public void setUpdated_on(Timestamp updated_on) {
    this.updated_on = updated_on;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "entity")
public Set<EntityAddress> getEntityAddresses() {
    return entityAddresses;
}

public void setEntityAddresses(Set<EntityAddress> entityAddresses) {
    this.entityAddresses = entityAddresses;
}

@IndexedEmbedded(depth = 1, prefix = "entityDetails")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "entity")
public Set<EntityDetails> getEntityDetails() {
    return entityDetails;
}

public void setEntityDetails(Set<EntityDetails> entityDetails) {
    this.entityDetails = entityDetails;
}
}

第二实体 -

@Entity
@Table(name = "raas_entity_details")
@Indexed
public class EntityDetails implements Serializable {

private static final long serialVersionUID = 2406650412424001390L;

private String entity_attribute;
private String attribute_type;
private String attribute_value;
private Integer primary_flag;
private String created_by;
private Timestamp created_on;
private String updated_by;
private Timestamp updated_on;
private CompositeKey compositeKey;
private EntityModel entity;
private Date from_date;
private Date to_date;


@EmbeddedId
@DocumentId
@FieldBridge(impl = PaddedObjectBridge.class)
public CompositeKey getCompositeKey() {
    return compositeKey;
}

public void setCompositeKey(CompositeKey compositeKey) {
    this.compositeKey = compositeKey;
}

@ContainedIn
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "entity_id", nullable=false, insertable=false, updatable= false)
public EntityModel getEntity() {
    return entity;
}

public void setEntity(EntityModel entity) {
    this.entity = entity;
}

public EntityDetails(){

}

public EntityDetails(String entity_attribute, String attribute_type,
        String attribute_value, Integer primary_flag, String created_by,
        Timestamp created_on, String updated_by, Timestamp updated_on) {
    super();
    this.entity_attribute = entity_attribute;
    this.attribute_type = attribute_type;
    this.attribute_value = attribute_value;
    this.primary_flag = primary_flag;
    this.created_by = created_by;
    this.created_on = created_on;
    this.updated_by = updated_by;
    this.updated_on = updated_on;
}

@Column(name = "entity_attribute", unique = false, nullable = true, length = 10)
public String getEntity_attribute() {
    return entity_attribute;
}

public void setEntity_attribute(String entity_attribute) {
    this.entity_attribute = entity_attribute;
}

@Column(name = "attribute_type", unique = false, nullable = true, length = 10)
public String getAttribute_type() {
    return attribute_type;
}

public void setAttribute_type(String attribute_type) {
    this.attribute_type = attribute_type;
}

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
@Column(name = "attribute_value", unique = false, nullable = true, length = 10)
public String getAttribute_value() {
    return attribute_value;
}

public void setAttribute_value(String attribute_value) {
    this.attribute_value = attribute_value;
}

@Column(name = "primary_flag", unique = false, nullable = true)
public Integer getPrimary_flag() {
    return primary_flag;
}

public void setPrimary_flag(Integer primary_flag) {
    this.primary_flag = primary_flag;
}

@Column(name = "created_by", unique = false, nullable = true, length = 10)
public String getCreated_by() {
    return created_by;
}

public void setCreated_by(String created_by) {
    this.created_by = created_by;
}

@Column(name = "created_on", unique = false, nullable = true, length = 10)
public Timestamp getCreated_on() {
    return created_on;
}

public void setCreated_on(Timestamp created_on) {
    this.created_on = created_on;
}

@Column(name = "updated_by", unique = false, nullable = true, length = 10)
public String getUpdated_by() {
    return updated_by;
}

public void setUpdated_by(String updated_by) {
    this.updated_by = updated_by;
}

@Column(name = "updated_on", unique = false, nullable = true, length = 10)
public Timestamp getUpdated_on() {
    return updated_on;
}

public void setUpdated_on(Timestamp updated_on) {
    this.updated_on = updated_on;
}

@Column(name = "from_date", unique = false, nullable = true, length = 10)
public Date getFrom_date() {
    return from_date;
}

public void setFrom_date(Date from_date) {
    this.from_date = from_date;
}

@Column(name = "to_date", unique = false, nullable = true, length = 10)
public Date getTo_date() {
    return to_date;
}

public void setTo_date(Date to_date) {
    this.to_date = to_date;
}

@Embeddable
public static class CompositeKey implements Serializable {

    private static final long serialVersionUID = 4630594723349289152L;
    private Integer id;
    private Integer entity_id;

    public CompositeKey(){

    }

    public CompositeKey(Integer id, Integer entity_id) {
        super();
        this.id = id;
        this.entity_id = entity_id;
    }

    public Integer getEntity_id() {
        return entity_id;
    }

    public void setEntity_id(Integer entity_id) {
        this.entity_id = entity_id;
    }

    //@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

}
}

需要一个休眠搜索查询,如下面的条件查询:

public static List<EntityModel> getEntityJoin(Session session, String searchString) {

Criteria cri = session.createCriteria(EntityModel.class, "entity").createAlias
  ("entity.entityDetails","entityd").setFetchMode("EntityDetails", FetchMode.JOIN);

    Disjunction or = Restrictions.disjunction();
    or.add(Restrictions.like("entity.middle_name", "%"+searchString+"%"));
    or.add(Restrictions.like("entity.first_name", "%"+searchString+"%"));
    or.add(Restrictions.like("entity.last_name", "%"+searchString+"%"));
    or.add(Restrictions.like("entity.short_name", "%"+searchString+"%"));
    or.add( Restrictions.and(
            Restrictions.eq( "entityd.entity_attribute", "Entity Name"),
            Restrictions.like("entityd.attribute_value", "%"+searchString+"%")
        ) );
    cri.add(or);
    cri.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<EntityModel> list = cri.list();
    return list;
}

这是基于条件的查询构建,我想要基于上述查询的休眠搜索查询。

1 个答案:

答案 0 :(得分:1)

首先,在@Field课程的entity_attribute上添加缺少的EntityDetails注释。根据您的查询,我假设您需要在此字段上进行完全匹配,因此您应该添加此注释:

  

@Field(name =“entity_attribute_unanalyzed”,analyze = Analyze.NO)

然后运行下一个查询(应该完成这项工作):

FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
QueryBuilder qb = fullTextSession.getSearchFactory()
    .buildQueryBuilder().forEntity( EntityModel.class ).get();

//Create the main BooleanJunction
BooleanJunction bool = qb.bool();

bool.should(qb.keyword().onField("middle_name").matching(searchString).createQuery());
bool.should(qb.keyword().onField("first_name").matching(searchString).createQuery());
bool.should(qb.keyword().onField("last_name").matching(searchString).createQuery());
bool.should(qb.keyword().onField("short_name").matching(searchString).createQuery());

//create another BooleanJunction for the alias conjunction
BooleanJunction aliasBool = qb.bool();
aliasBool.must(qb.keyword().onField("entityDetails.entity_attribute_unanalyzed")
                           .matching("Entity Name").createQuery());
aliasBool.must(qb.keyword().onField("entityDetails.attribute_value")
                           .matching(searchString).createQuery());

//add the aliasBool to the main bool
bool.should(aliasBool.createQuery());

//execute query
org.apache.lucene.search.Query luceneQuery = bool.createQuery();
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery , EntityModel.class);

// execute search
List<EntityModel> list = hibQuery.list();

tx.commit();
session.close();