Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA(EclipseLink 2.4)不总是加载子对象_Jpa_Eclipselink - Fatal编程技术网

JPA(EclipseLink 2.4)不总是加载子对象

JPA(EclipseLink 2.4)不总是加载子对象,jpa,eclipselink,Jpa,Eclipselink,我正在使用Glassfish 3.1.2和集成的EclipseLink JPA实现。我的问题是,特定父对象的子对象并不总是被加载(有时加载,有时不加载)。我还使用Jax-RS和Jax-b。我还使用SQLite作为数据库。 奇怪的是,如果我重新启动glassfish(几次),或者只是刷新我的页面几次,所有东西都会正常加载 @Entity @XmlRootElement public class Location { @Id @GeneratedValue(strategy=Gen

我正在使用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;
    }
我要做的是从数据库中获取所有位置,这些位置与我在url中传递的id所在的位置具有相同的名称。我尝试使用内部连接、左连接、左连接获取等操作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"}}}

如果您认为ManyToOne是懒惰的,那么它可能不会被加载,但是由于默认值是渴望,您应该看到啤酒总是被加载的。检查您的查询结果,getBeer()是否返回一些内容?我也在考虑加载模式,但我不确定这是问题所在。现在我在控制台中添加了out.print打印位置对象,问题就解决了。但是,这仍然不是一个合适的解决方案。我尝试从db中选择其他位置,发现打印它们或任何子对象并不能解决这个问题。您是对的,getBeer()不会返回任何内容。子对象有时只是没有加载,尽管guery是相同的。
 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
{"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"}}}