Java 进入休眠状态

Java 进入休眠状态,java,hibernate,Java,Hibernate,我有一节乡村课 public class Country implements java.io.Serializable { private Integer countryId; private String countryName; private boolean status; private Set<State> states = new HashSet<>(0); .. .. (getters and setters) } 如何通过编写获取Country类的函数

我有一节乡村课

public class Country implements java.io.Serializable
{
private Integer countryId;
private String countryName;
private boolean status;
private Set<State> states = new HashSet<>(0);
..
..
(getters and setters)
}
如何通过编写获取Country类的函数,即

public List<Country> getCountryList() 
{
try
{
List<Country> countryList = new ArrayList<>();
Criteria crit HibernateUtil.createCriteria(Country.class,"country").add(Restrictions.eq("status", true));
crit.addOrder(Order.asc("countryName"));
List<?> list = crit.list();
Iterator<?> iterator = list.iterator();
while (iterator.hasNext()) 
{
Country country = (Country)iterator.next();
countryList.add(country);
}
return countryList;
}
}

我需要在上述函数中做哪些更改,以便如果我从country对象获取状态集,我只获取状态为true的状态

试试看,这可能对你有用

public class Country implements java.io.Serializable
{
private Integer countryId;
private String countryName;
private boolean status;
@Whereclause=状态=真

私有集状态=新HashSet0


Hibernate不会这样做,它没有任何意义/绕过了ORM的全部目的。您可以在实际国家/地区模型的集合上添加@Where限制,以限制集合的内容。您不能。一个国家的州就是这个国家的州。不是这个国家的一些州。如果你想得到一个国家的一些州,你需要一个查询州的查询。但是这将始终存储状态为true的州。在某种情况下,我可能希望看到一个国家的所有活跃和不活跃状态。这不是一个有效的测试用例吗@马赫什
public class Country implements java.io.Serializable
{
private Integer countryId;
private String countryName;
private boolean status;
..
..
(getters and setters)
}