Hibernate 5按示例查询添加条件

时间:2016-09-13 19:19:27

标签: java mysql hibernate hibernate-criteria

所以,我使用的是最新的Hibernate 5.x版本。我试图通过示例进行简单的查询。

这是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
 ...'
}

以下是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;
}

这是测试它的测试:

 @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);
}

我知道数据库记录,应该至少返回一条记录,但在这种情况下,我没有得到任何数据。

以下是执行的实际查询:

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=?)

如果你注意到where子句,它会说:

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

它应该只是:

where (this_.company_city=?)

因此,额外的标准是弄乱我的查询,我无法得到正确的结果。

我想知道如何纠正和解决这个问题?我多次搜索谷歌,并在我来到这里之前先检查了hibernate论坛。 非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:3)

字段company_enablededited_byentered_by是基元,因此它们在创建时具有值,对于布尔值,它是假的,并且长期它是0.所以当对象是在那里创建了值,当你调用过滤器时,hibernate会看到字段值并将它们放在查询中。