我正在使用Glassfish 3.1.2和集成的EclipseLink JPA实现。我的问题是特定父级的子对象并不总是被加载(有时它是,有时它不是)。我也在使用Jax-RS和Jax-b。我也使用SQLite作为数据库。 奇怪的是,如果我重新启动glassfish(几次),或者几次刷新我的页面,一切都正常加载。
@Entity
@XmlRootElement
public class Location {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private int id;
@NotNull
private String name;
@NotNull
private String lat;
@NotNull
private String lon;
@ManyToOne
@NotNull
private User user;
@ManyToOne
@NotNull
private Beer beer;
// getters & setters
@Entity
@XmlRootElement
public class Beer {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
@NotNull
private String name;
@NotNull
private String description;
@NotNull
private Date added;
@NotNull
private String pic;
private int rateAroma; // type1
private int rateAlcoholContent; // type2
private int rateFlavour; // type3
@OneToMany(mappedBy = "beer")
private Collection<Comment> comments;
@OneToMany(mappedBy = "beer")
private Collection<Rate> ratings;
@OneToMany(mappedBy = "beer")
private Collection<Location> locations;
@ManyToMany
@JoinTable(joinColumns = @JoinColumn(name = "BEER_ID", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "id"))
private Collection<User> users;
// I've only included get/set methods of attributes which are marked @XmlTransient
@XmlTransient
public Collection<Comment> getComments() {
return comments;
}
@XmlJavaTypeAdapter(SqlDateAdapter.class)
public Date getAdded() {
return added;
}
@XmlTransient
public Collection<Rate> getRatings() {
return ratings;
}
@XmlTransient
public Collection<Location> getLocations() {
return locations;
}
@XmlTransient
public Collection<User> getUsers() {
return users;
}
REST服务
@GET
@Path("/beers/{id}")
@Produces(MediaType.APPLICATION_JSON)
public List<Location> getMarkedLocations(@PathParam("id") int locationId) {
String locationName = null;
try {
locationName = locdao.getLocation(locationId).getName();
} catch (Exception e) {
System.out.println("no such location");
return null;
}
List<Location> locations = locdao.getLocationsAndBeers(locationName);
return locations;
}
LocationDAO
public List<Location> getLocationsAndBeers(String param) {
em = DB.getDBFactory().createEntityManager();
List<Location> locations = null;
try {
Query q = em.createQuery("SELECT l FROM Location l WHERE l.name = :name", Location.class);
q.setParameter("name",param);
locations = (List<Location>)q.getResultList();
} catch (Exception e) {
System.out.println(e.toString());
} finally {
em.close();
}
return locations;
}
数据库
ID LOCATION_NAME BEER_ID USER_ID
10 location name 3 1
11 location name 4 2
60 location name 53 51
61 location name 54 52
62 beer bar 3 1
我要做的是从数据库获取所有位置,这些位置与我在url中传递的id的位置相同。我尝试使用INNER JOINS,LEFT JOINS,LEFT JOINS FETCH等来操纵我的SQL,但问题仍然存在。
这是我应该得到的JSON如果位置ID是61
{"location":{"beer":{"added":"2013-04-29 18:17:39","description":"DESC","id":"3","name":"beer_name","pic":"url b1","rateAlcoholContent":"3","rateAroma":"4","rateFlavour":"3"},"id":"62","name":"beer bar","user":{"email":"u1@email","fbid":"0","id":"1","lastName":"lastname u1","name":"name u1","username":"u1"}}}
我有时会得到什么。没有啤酒对象。
{"location":{"id":"62","name":"beer bar","user":{"email":"u1@email","fbid":"0","id":"1","lastName":"lastname u1","name":"name u1","username":"u1"}}}