我有一个包含两个表(City,Country)的数据库以及它们之间的两个关系。
第一个是"一个国家有多个城市"关系。
select * from City inner join Country on(code=countrycode);
第二个是"一个国家有一个资本"关系。
select * from City inner join Country on(capital=id);
Hibernate标准适用于第一种关系,例如:
Criteria criteria = session.createCriteria(City.class);
List<City> = criteria
.createCriteria("countrycode")
.add(Restrictions.eq("continent", continent))
.list();
但是如何创建一个Hibernate标准来检索大写字母列表呢?第二个关系是未映射的。 Hibernate是否支持这种关系?
城市课程:
@Entity
public class City {
@Id
@Column(name = "ID", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "countrycode", nullable = false)
private Country countrycode;
...
国家课程:
@Entity
public class Country {
@Id
private String code;
@OneToMany(targetEntity = City.class, mappedBy = "countrycode")
private Set<City> cities;
private Integer capital;
...
答案 0 :(得分:0)
该城市是否是任何国家的首都的信息必须在城市内。向City添加一个布尔字段,如下所示:
List<City> = session.createCriteria(City.class)
.createCriteria("countrycode")
.add(Restrictions.eq("continent", continent))
.add(Restrictions.eq("capital", true))
.list();