【发布时间】:2017-03-28 14:02:10
【问题描述】:
我在尝试获取 Json 结果时遇到了一个小问题,我有 2 个表(路线和目的地),结构如下:
路线: route table
和目的地: destination table
Route.class
@Entity
@XmlRootElement
public class Route {
private int idroute;
private String routename;
private Double price;
private String description;
private String city;
private Collection<Destination> destinationsByIdroute;
@Id
@Column(name = "idroute", nullable = false)
public int getIdroute() {
return idroute;
}
public void setIdroute(int idroute) {
this.idroute = idroute;
}
@Basic
@Column(name = "routename", nullable = true, length = 45)
public String getRoutename() {
return routename;
}
public void setRoutename(String routename) {
this.routename = routename;
}
@Basic
@Column(name = "price", nullable = true, precision = 2)
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Basic
@Column(name = "description", nullable = true, length = 500)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Basic
@Column(name = "city", nullable = true, length = 30)
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Route route = (Route) o;
if (idroute != route.idroute) return false;
if (routename != null ? !routename.equals(route.routename) : route.routename != null) return false;
if (price != null ? !price.equals(route.price) : route.price != null) return false;
if (description != null ? !description.equals(route.description) : route.description != null) return false;
if (city != null ? !city.equals(route.city) : route.city != null) return false;
return true;
}
@Override
public int hashCode() {
int result = idroute;
result = 31 * result + (routename != null ? routename.hashCode() : 0);
result = 31 * result + (price != null ? price.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (city != null ? city.hashCode() : 0);
return result;
}
@OneToMany(mappedBy = "routeByIdroute")
public Collection<Destination> getDestinationsByIdroute() {
return destinationsByIdroute;
}
public void setDestinationsByIdroute(Collection<Destination> destinationsByIdroute) {
this.destinationsByIdroute = destinationsByIdroute;
}
}
目的地类:
@Entity
public class Destination {
private int iddestination;
private String frompoint;
private String topoint;
private Route routeByIdroute;
@Id
@Column(name = "iddestination", nullable = false)
public int getIddestination() {
return iddestination;
}
public void setIddestination(int iddestination) {
this.iddestination = iddestination;
}
@Basic
@Column(name = "frompoint", nullable = false, length = 45)
public String getFrompoint() {
return frompoint;
}
public void setFrompoint(String frompoint) {
this.frompoint = frompoint;
}
@Basic
@Column(name = "topoint", nullable = false, length = 45)
public String getTopoint() {
return topoint;
}
public void setTopoint(String topoint) {
this.topoint = topoint;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Destination that = (Destination) o;
if (iddestination != that.iddestination) return false;
if (frompoint != null ? !frompoint.equals(that.frompoint) : that.frompoint != null) return false;
if (topoint != null ? !topoint.equals(that.topoint) : that.topoint != null) return false;
return true;
}
@Override
public int hashCode() {
int result = iddestination;
result = 31 * result + (frompoint != null ? frompoint.hashCode() : 0);
result = 31 * result + (topoint != null ? topoint.hashCode() : 0);
return result;
}
@ManyToOne
@JoinColumn(name = "idroute", referencedColumnName = "idroute")
public Route getRouteByIdroute() {
return routeByIdroute;
}
public void setRouteByIdroute(Route routeByIdroute) {
this.routeByIdroute = routeByIdroute;
}
}
RouteService.class
public class RouteService extends HibernateUtils {
public static List<Route> getAllRoutes() {
Session session = getSessionFactory().openSession();
List<Route> routes = new ArrayList<Route>();
try {
System.out.println("querying all the managed entities...");
session.beginTransaction();
routes = session.createQuery("select r from Route r join fetch r.destinationsByIdroute d", Route.class).list();
session.getTransaction().commit();
}catch(HibernateException hbe){
hbe.printStackTrace();
}
finally {
session.close();
}
return routes;
}
}
还有 MyResource.class
@Path("/myresource")
public class MyResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Route> getRoutes() {
List<Route> route = null;
try{
route = RouteService.getAllRoutes();
}catch (Exception e){
e.printStackTrace();
}
return route;
}
}
此代码有效,但如果我的路由表中只有 1 条记录,我不知道为什么会给我 3 个结果:
[
{
"city": "Santo Domingo",
"description": "los coquitos, farmacia enriquillo",
"destinationsByIdroute": [
{
"frompoint": "18.479081, -69.978025",
"iddestination": 3,
"topoint": "18.477886, -69.968940"
},
{
"frompoint": "18.470278, -69.994422",
"iddestination": 1,
"topoint": "18.473354, -69.993565"
},
{
"frompoint": "18.473354, -69.993565",
"iddestination": 2,
"topoint": "18.479081, -69.978025"
}
],
"idroute": 1,
"price": 30,
"routename": "Las caobas-Kilometro 9"
}, { “城市”:“圣多明哥”, "description": "los coquitos, farmacia enriquillo", “destinationsByIdroute”:[ { “从点”:“18.479081,-69.978025”, “目标”:3, “topoint”:“18.477886,-69.968940” }, { “从点”:“18.470278,-69.994422”, “目标”:1, “顶点”:“18.473354,-69.993565” }, { “从点”:“18.473354,-69.993565”, “目标”:2, “顶点”:“18.479081,-69.978025” } ], “idroute”:1, “价格”:30, "routename": "Las caobas-Kilometro 9" }, { “城市”:“圣多明哥”, "description": "los coquitos, farmacia enriquillo", “destinationsByIdroute”:[ { “从点”:“18.479081,-69.978025”, “目标”:3, “topoint”:“18.477886,-69.968940” }, { “从点”:“18.470278,-69.994422”, “目标”:1, “顶点”:“18.473354,-69.993565” }, { “从点”:“18.473354,-69.993565”, “目标”:2, “顶点”:“18.479081,-69.978025” } ], “idroute”:1, “价格”:30, "routename": "Las caobas-Kilometro 9" } ]
我想要这样的东西:
{
"city": "Santo Domingo",
"description": "los coquitos, farmacia enriquillo",
"destinationsByIdroute": [
{
"frompoint": "18.479081, -69.978025",
"iddestination": 3,
"topoint": "18.477886, -69.968940"
},
{
"frompoint": "18.470278, -69.994422",
"iddestination": 1,
"topoint": "18.473354, -69.993565"
},
{
"frompoint": "18.473354, -69.993565",
"iddestination": 2,
"topoint": "18.479081, -69.978025"
}
],
"idroute": 1,
"price": 30,
"routename": "Las caobas-Kilometro 9"
}
如果你看到的是相同的结果,但只有一次是我想要的,而不是像第一个示例那样的 3 次。
有什么建议吗?提前致谢。
【问题讨论】:
-
我可以假设您的目标表中有 3 条记录,因为您根本没有任何条件过滤来连接。 routes = session.createQuery("select r from Route r join fetch r.destinationsByIdroute d", Route.class).list();
-
@Chris Thompson,是的,我的目标表有 3 条记录,我不知道如何编写 hql systax 来获得我想要的,有什么建议吗?
标签: mysql json hibernate rest jersey