mysql - 如何使用带有双向实体的 RestFul Web 服务正确编写 Json?

标签 mysql json hibernate rest jersey

我在尝试获取 Json 结果时遇到一个小问题,我有 2 个表(路线和目的地),其结构如下:

路线: route table

目的地: destination table

路线.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", “目的地ByIdroute”:[ { “从点”:“18.479081,-69.978025”, “我的目​​的地”:3, “到点”:“18.477886,-69.968940” }, { “从点”:“18.470278,-69.994422”, “身份目的地”:1, “到点”:“18.473354,-69.993565” }, { “从点”:“18.473354,-69.993565”, “身份目的地”:2, “到点”:“18.479081,-69.978025” } ], “id路由”:1, “价格”:30, "routename": "拉斯考巴斯-9 公里" }, { “城市”:“圣多明各”, "description": "los coquitos, farmacia enriquillo", “目的地ByIdroute”:[ { “从点”:“18.479081,-69.978025”, “我的目​​的地”:3, “到点”:“18.477886,-69.968940” }, { “从点”:“18.470278,-69.994422”, “身份目的地”:1, “到点”:“18.473354,-69.993565” }, { “从点”:“18.473354,-69.993565”, “身份目的地”:2, “到点”:“18.479081,-69.978025” } ], “id路由”:1, “价格”:30, "routename": "拉斯考巴斯-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 次。

有什么建议吗?提前致谢。

最佳答案

我认为你可以做这样的事情:

session.createQuery("select r from Route r join fetch r.destinationsByIdroute d WHERE r.idroute = d.idroute", Route.class);

但是您最终可能希望将路由传递到此方法中,并使用参数过滤路由。 但我认为这会将您的目的地结果过滤为仅一条记录。

关于mysql - 如何使用带有双向实体的 RestFul Web 服务正确编写 Json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40597122/

相关文章:

c# - MySQL中只取第一个满足第二个条件的

javascript - 短语对象文字表示法中的 "literal"是什么意思?

mysql - 如何在 Windows 中使用 RMySQL?

mysql - 对值对的唯一约束,MySQL

java - 如何在 Struts 2 中验证失败时返回自定义响应

spring - 同一张表的 innerJoin 的 Querydsl

java - 多数据库应用SpringBoot、Hibernate和JPA

hibernate - JBoss 在开发环境中找不到 PostgreSQL 的 JDBC 驱动程序

MySql DateDiff 不返回负数

php - Prestashop Web 服务返回 JSON