0

我正在使用带有集成 EclipseLink JPA 实现的 Glassfish 3.1.2。我的问题是特定父对象的子对象并不总是被加载(有时是,有时不是)。我也在使用 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;
    }

休息服务

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

位置DAO

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"}}}

我有时会得到什么。没有 Beer 对象。

{"location":{"id":"62","name":"beer bar","user":{"email":"u1@email","fbid":"0","id":"1","lastName":"lastname u1","name":"name u1","username":"u1"}}}
4

0 回答 0