2016-11-14 50 views
0

我有一個小問題試圖獲得一個JSON結果,我有2個表(路線和目的地)結構如下:如何正確使用帶有雙向實體的RestFul Web服務編寫Json?

路線: 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; 
} 
} 

Destinatio類:

@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; 
} 

}

這種合作去工作,但我不知道爲什麼給我3分的結果,如果我只有僅1個記錄在我的路由表:

[ 
{ 
"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" 

}, { 「城市」:「聖多明各」, 「說明」: 「洛斯coquitos,FARMACIA恩裏基」, 「destinationsByIdroute」:[{ 「frompoint」: 「18.479081,-69.978025」, 「iddestination」:3, 「尖山」: 「18.477886,-69.968940」 }, 「frompoint」:「18.470278,-69.994422」, 「iddestination」:1, 「尖山」: 「18.473354,-69.993565」 },{ 「frompoint」: 「18.473354,-69.993565」, 「iddestination」:2, 「尖山」: 「18.479081,-69.978025」 } ] , 「idroute」:1, 「價格」:30, 「routename」: 「拉斯caobas-Kilometro 9」 }, { 「城市」: 「聖多明各」, 「描述」:「洛斯coquitos ,farmacia enriquillo「, 」destinationsByIdroute「:[ { 」frompoint「:」18.479081,-69.978025「, 」iddestination「:3, 「尖山」: 「18.477886,-69.968940」 },{ 「frompoint」: 「18.470278,-69.994422」, 「iddestination」:1, 「尖山」: 「18.473354,-69.993565」 }, 「frompoint」:「18.473354,-69.993565」, 「iddestination」:2, 「topoint」:「18.479081,-69。978025" } ], 「idroute」:1, 「價」:30, 「routename」: 「拉斯維加斯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倍,如第一個例子。

有何建議?提前致謝。

+0

我可以假設你有3條記錄在你的目的地表中,因爲你沒有任何標準過濾條件。 routes = session.createQuery(「select r from Route r join fetch r.destinationsByIdroute d」,Route.class).list(); – ChrisThompson

+0

@Chris Thompson,是的,我的目標表有3條記錄,我不確定如何編寫hql systax來獲得我想要的,有什麼建議嗎? –

回答

0

我想你可以這樣做:

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

但是你會最終想大概的路線傳遞到與參數這種方法和過濾路由器。 但我認爲這會過濾您的目標結果只有一個記錄。

相關問題