简体   繁体   English

Hibernate 5 Query By Example添加标准

[英]Hibernate 5 Query By Example adds criteria

So, I am using the latest Hibernate 5.x version. 所以,我使用的是最新的Hibernate 5.x版本。 I am trying to do a simple Query By Example. 我试图通过示例进行简单的查询。

Here is the hibernate entity class: 这是hibernate实体类:

@SuppressWarnings("serial")
@Entity
@Table(name = "company")
public class CompanyEntity implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "company_id")
private long companyId;

// `account_enabled` varchar(15) NOT NULL,
@Column(name = "company_enabled", columnDefinition = "BIT")
private boolean companyEnabled;

@Column(name = "company_name")
private String companyName;

@Column(name = "company_address1")
private String address1;

@Column(name = "company_address2")
private String address2;

@Column(name = "company_city")
private String addressCity;

@Column(name = "company_state")
private String addressState;

@Column(name = "company_postal_code")
private String addressPostalCode;

@Column(name = "company_country")
private String addressCountry;

@Column(name = "company_phone")
private String phone;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "company_parent")
private CompanyEntity parent;

@Column(name = "entered_by")
private long enteredBy;

@Column(name = "entered_date")
@Temporal(TemporalType.TIMESTAMP)
private Date enteredDate;

@Column(name = "edited_by")
private long editedBy;

@Column(name = "edited_date")
@Temporal(TemporalType.TIMESTAMP)
private Date editedDate;

... getters, setters, hashCode, equals, toString 
    as auto-generated by Eclipse
 ...'
}

and here is the query by example in the Dao: 这里是Dao中的示例查询:

@Override
public List<CompanyEntity> getCompanyEntityByExample(CompanyEntity exampleEntity)
{
    Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(CompanyEntity.class).add(Example.create(exampleEntity));

    List<CompanyEntity> companyEntityList = criteria.list();

    System.out.println("getCompanyEntityByExample: companyEntityList: size=" + companyEntityList.size());
    System.out.println("getCompanyEntityByExample: companyEntityList=" + companyEntityList);

    return companyEntityList;
}

and here is the test to test this out: 这是测试这个的测试:

 @Test
public void testGetCompanyByExample()
{
    String addressCity = "Boston";
    CompanyEntity exampleEntity = new CompanyEntity();
    exampleEntity.setAddressCity(addressCity);
    List<CompanyEntity> companyList = companyDao.getCompanyEntityByExample(exampleEntity);
    assertNotNull(companyList);
    assertEquals(true, companyList.size() == 1);
}

I know with the database records, there should be at least one record returned, but in this case, I don't get any data. 我知道有了数据库记录,应该至少返回一条记录,但在这种情况下,我没有得到任何数据。

Here is the actual query getting executed: 这是执行的实际查询:

select this_.company_id as company_1_1_1_, this_.company_address1 as company_2_1_1_, this_.company_address2 as company_3_1_1_, this_.company_city as company_4_1_1_, this_.company_country as company_5_1_1_, this_.company_postal_code as company_6_1_1_, this_.company_state as company_7_1_1_, this_.company_enabled as company_8_1_1_, this_.company_name as company_9_1_1_, this_.edited_by as edited_10_1_1_, this_.edited_date as edited_11_1_1_, this_.entered_by as entered12_1_1_, this_.entered_date as entered13_1_1_, this_.company_parent as company15_1_1_, this_.company_phone as company14_1_1_, companyent2_.company_id as company_1_1_0_, companyent2_.company_address1 as company_2_1_0_, companyent2_.company_address2 as company_3_1_0_, companyent2_.company_city as company_4_1_0_, companyent2_.company_country as company_5_1_0_, companyent2_.company_postal_code as company_6_1_0_, companyent2_.company_state as company_7_1_0_, companyent2_.company_enabled as company_8_1_0_, companyent2_.company_name as company_9_1_0_, companyent2_.edited_by as edited_10_1_0_, companyent2_.edited_date as edited_11_1_0_, companyent2_.entered_by as entered12_1_0_, companyent2_.entered_date as entered13_1_0_, companyent2_.company_parent as company15_1_0_, companyent2_.company_phone as company14_1_0_ from company this_ left outer join company companyent2_ on this_.company_parent=companyent2_.company_id where (this_.company_city=? and this_.company_enabled=? and this_.edited_by=? and this_.entered_by=?)

If you notice the where clause, it says: 如果您注意到where子句,它会说:

where (this_.company_city=? and this_.company_enabled=? and this_.edited_by=? and this_.entered_by=?)

and it should be only: 它应该只是:

where (this_.company_city=?)

So, the extra criteria is messing with my query and I am unable to get the correct results. 所以,额外的标准是搞乱我的查询,我无法得到正确的结果。

I was wondering how I can correct and fix this? 我想知道如何纠正和解决这个问题? I searched several times against Google, and checked the hibernate forums first before I came here. 我多次搜索谷歌,并在我来到这里之前先检查了hibernate论坛。 Any help is much appreciated. 任何帮助深表感谢。

Thanks! 谢谢!

字段company_enablededited_byentered_by是原语,因此它们在创建时有一个值,对于boolean它是false,并且很长时间它是0.因此,当创建对象时,值就在那里,当你调用过滤器时,hibernate会看到字段值并将它们放在查询上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM