简体   繁体   English

Hibernate查询示例(来自Spring 3)

[英]Hibernate query by example (from Spring 3)

I've made my entity classes Adress , Road and County . 我已经制作了我的实体课AdressRoadCounty A Road is in a County and an Adress in on a Road . 一个Road是在CountyAdress在上Road I would like to list all the Adresses in a County . 我想列出一个County所有Adresses Therefore, in my AdressService I say: 因此,在我的AdressService我说:

public List<Adress> AllAdresses(County county) {
  Adress adress = new Adress();
  Road road = new Road();
  road.setCounty(county);
  adress.setRoad(road);

  Example example = Example.create(adress);
  return (List<Adress>) adressDAO.query(Adress.class, example);
}

In my AdressDAO I have query(): 在我的AdressDAO我有query():

public List query(Class<?> c, Example example) {
  return getSession().createCriteria(c).add(example).setMaxResults(100).list();
}

This executes the following query on my database server: 这将在我的数据库服务器上执行以下查询:

select this_.AdressId as AdressId2_0_, 
       this_.Description as Descript3_2_0_, 
       this_.DescriptionShort as Descript4_2_0_, 
       this_.HouseLetter as HouseLetter2_0_, 
       this_.HouseNr as HouseNr2_0_, 
       this_.RoadId as RoadId2_0_ 
from tblAdress this_ 
where (this_.HouseNr=0) 
limit 100

I had expected it to at least include SOME information about my entity County , and an inner join with tblRoad . 我原以为它至少包含了关于我的实体County一些信息,以及一个带有tblRoad的内部tblRoad tblRoad has a primary key roadId , so I expected this_.roadId to be joined with tblRoad.roadId , and I expected tblRoad.countyId to be set to the primary key of County , that is countyId . tblRoad有一个主键roadId ,所以我希望this_.roadIdtblRoad.roadId ,我希望将tblRoad.countyId设置为County的主键,即countyId

Why is the query in this example not built correctly when I use my own entity types? 当我使用自己的实体类型时,为什么这个例子中的查询没有正确构建? If I only use integers and strings, they work fine, but not entities. 如果我只使用整数和字符串,它们工作正常,但不是实体。 How do I make joins like this work with my own entities? 如何使这样的连接与我自己的实体一起工作?

From the Hibernate docs : 来自Hibernate文档

Version properties, identifiers and associations are ignored 版本属性,标识符和关联将被忽略

And that, as they say, is that. 而且,正如他们所说的那样。

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

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