【发布时间】:2016-08-01 09:59:46
【问题描述】:
问题说明:Country与State有收藏关系。获取一个国家,将其所有州与其关联。在许多情况下是需要的。现在在给定的情况下,我有国家id 和国家id,当使用国家ID 获取国家过滤时,是否有可能在Country 中获得单个State?
Country.class
@Entity
@Repository
@Table(name="COUNTRY")
class Country implements Serializable{
@Id
@GeneratedValue(startegy=GenerationType.AUTO)
private long id;
@OneToMany(mappedBy="country", cascade={javax.persistence.CascadeType.ALL}, fetch=FetchType.EAGER)
private java.util.Set<State> states;
//Getters and setters
}
State.class
@Entity
@Repository
@Table(name="STATE")
class State implements Serializable{
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false, unique=true, updatable=true)
private long @Id
@ManyToOne
@JoinColumn(name="id")
private Country country;
//Getter and setter
}
我尝试过以下查询:
//This query works great when used in SQL editor. Getting one state
select * from country left join state ON country.id = state.id and state.id=3
//Tried following
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
在 SQL 查询上使用并创建了条件,但我得到了所有状态。或者这个问题可以解决吗?我们有更好的选择吗?
【问题讨论】:
标签: hibernate hibernate-mapping