Sql 具有映射实体的HQL查询 @实体(name=“locations”) @表(name=“位置”) 公共类位置{ @身份证 @生成值 @列(name=“id”) 私人长id; @列(name=“name”) @NotEmpty(message=“El Nombre es obligatorio”)@Size(最小值=3,最大值=150,message=“El Nombre debe tener entre 3 y 150克拉”) 私有字符串名称; @OneToMany(mappedBy=“位置”) 私人体育赛事名单; } @实体(名称=“体育”) @表(name=“sports”) 公共体育课{ @身份证 @生成值 @列(name=“id”) 私人长id; @列(name=“name”) @NotEmpty(message=“El Nombre es obligatorio”)@Size(最小值=3,最大值=150,message=“El Nombre debe tener entre 3 y 150克拉”) 私有字符串名称; @列(name=“active”) 私有布尔活动; @OneToMany(mappedBy=“sport”)//名称为otra entidad可变 私人体育赛事; } @实体(名称=“体育赛事”) @表(name=“体育赛事”) 公营体育活动{ @身份证 @生成值 @列(name=“id”) 私人长id; @列(name=“name”) @NotEmpty(message=“El Nombre es obligatorio”)@Size(最小值=3,最大值=150,message=“El Nombre debe tener entre 3 y 150克拉”) 私有字符串名称; @许多酮 @JoinColumn(name=“sport\u id”) 私人体育; @许多酮 @JoinColumn(name=“location\u id”) 私人位置; }
查询:Sql 具有映射实体的HQL查询 @实体(name=“locations”) @表(name=“位置”) 公共类位置{ @身份证 @生成值 @列(name=“id”) 私人长id; @列(name=“name”) @NotEmpty(message=“El Nombre es obligatorio”)@Size(最小值=3,最大值=150,message=“El Nombre debe tener entre 3 y 150克拉”) 私有字符串名称; @OneToMany(mappedBy=“位置”) 私人体育赛事名单; } @实体(名称=“体育”) @表(name=“sports”) 公共体育课{ @身份证 @生成值 @列(name=“id”) 私人长id; @列(name=“name”) @NotEmpty(message=“El Nombre es obligatorio”)@Size(最小值=3,最大值=150,message=“El Nombre debe tener entre 3 y 150克拉”) 私有字符串名称; @列(name=“active”) 私有布尔活动; @OneToMany(mappedBy=“sport”)//名称为otra entidad可变 私人体育赛事; } @实体(名称=“体育赛事”) @表(name=“体育赛事”) 公营体育活动{ @身份证 @生成值 @列(name=“id”) 私人长id; @列(name=“name”) @NotEmpty(message=“El Nombre es obligatorio”)@Size(最小值=3,最大值=150,message=“El Nombre debe tener entre 3 y 150克拉”) 私有字符串名称; @许多酮 @JoinColumn(name=“sport\u id”) 私人体育; @许多酮 @JoinColumn(name=“location\u id”) 私人位置; },sql,spring,hibernate,hql,Sql,Spring,Hibernate,Hql,查询: @Entity(name = "locations") @Table(name="locations") public class Location { @Id @GeneratedValue @Column(name="id") private long id; @Column(name="name") @NotEmpty(message="El Nombre es obligatorio") @Size(min=3, max=150, message="El No
@Entity(name = "locations")
@Table(name="locations")
public class Location {
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="name")
@NotEmpty(message="El Nombre es obligatorio") @Size(min=3, max=150, message="El Nombre debe tener entre 3 y 150 caracteres")
private String name;
@OneToMany(mappedBy="location")
private List<SportEvent> sportevents;
}
@Entity(name = "sports")
@Table(name = "sports")
public class Sport {
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="name")
@NotEmpty(message="El Nombre es obligatorio") @Size(min=3, max=150, message="El Nombre debe tener entre 3 y 150 caracteres")
private String name;
@Column(name="active")
private Boolean active;
@OneToMany(mappedBy="sport") //nombre de la varible de la otra entidad
private Set<SportEvent> sportevents;
}
@Entity(name = "sport_events")
@Table(name="sport_events")
public class SportEvent {
@Id
@GeneratedValue
@Column(name="id")
private long id;
@Column(name="name")
@NotEmpty(message="El Nombre es obligatorio") @Size(min=3, max=150, message="El Nombre debe tener entre 3 y 150 caracteres")
private String name;
@ManyToOne
@JoinColumn(name="sport_id")
private Sport sport;
@ManyToOne
@JoinColumn(name="location_id")
private Location location;
}
locations=(List)s.createQuery(“选择不同的l”+
“从位置l”+
“以lse身份加入l.sportevents”+
“其中lse.sport.id=:id”)
.setLong(“id”,运动id)
.list();
我想通过“运动”菜单筛选体育赛事的响应位置
通过下面的查询,我得到的是体育赛事的地点,但忽略id sport所在的条件
如果它正确地映射到已经有体育赛事的访问位置,我只需要执行加入条件运动,但不起作用
在听证会上,我写得很好:
locations = (List<Location>) s.createQuery("SELECT DISTINCT l " +
"FROM locations l " +
"JOIN l.sportevents AS lse " +
"WHERE lse.sport.id = :id")
.setLong("id", sportId)
.list();
在我的数据库中,我有:
体育:
身份证名称
1-sport1
2-sport2
3-sport3
地点
身份证名称
1-位置1
2-位置2
事件
id-name-sport\u id-location\u id
1-event1-2-1
2-事件2-2-1
3-事件3-1-2
4-事件4-1-2
5-event 5-3-1
6-event6-3-1
将位置添加到各自的赛事中,但属于特定的运动项目。对于当前查询,我只按位置获取事件。在我的网站中,我在所选运动的菜单中,我想查看有运动赛事的地点。对不起,您能尝试重新表述您的问题吗?我不明白你的问题是什么。你能试着将sql打印到控制台并粘贴到这里吗?我发现查询有问题,我想你不能使用lse.sport,因为在这种情况下lse是一个集合。我已经更新了帖子
<c:forEach var="location" items="${locations}">
<li>
<label class="tree-toggle nav-header"><i class="icon-hand-right"></i><a href="#">${location.name}</a></label>
<ul class="nav nav-list tree hide">
<c:forEach var="sportevent" items="${location.sportevents}">
<li><a href="#">${sportevent.name}</a></li>
</c:forEach>
</ul>
</li>
<li class="divider"></li>
</c:forEach>