在appengine中持久化/检索实体时,GoogleJsonResponse异常

时间:2014-08-12 05:58:05

标签: java android google-app-engine jpa

我正在使用appengine后端开发android应用程序。有两个实体类,User和Book,在双向一对多关系中。书本实体由User实体中的books(ArrayList)字段引用。当我尝试插入一个book实体并在客户端接收插入的实体时,我在客户端app.Exception消息中收到了GoogleJsonResponseException异常:

com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException:您刚刚尝试访问字段\“owner \”但是当您分离对象时,此字段未分离。要么不访问此字段,要么在分离对象时将其分离。

这就是我插入图书实体的方式:

    try {

            Book retrievedBook=endpoint.insertBook(registeredNumber, b).execute();
            if(retrievedBook!=null){
                String message="Success";

            }
        } catch (Exception e) {
            //THE ERROR IS CAUGHT HERE
            e.printStackTrace();

        }

UserEndpoint中的insertBook方法:

    @ApiMethod(name = "insertBook")
 public Book insertBook(Book book,@Named("userRegistrationNumber")String userRegistrationNumber) {
    EntityManager mgr = getEntityManager();
    try {
        UserEndpoint ue=new UserEndpoint();



        mgr.getTransaction().begin();

        User u=mgr.find(User.class, userRegistrationNumber);


        u.setBook(book);        

        mgr.flush();
        mgr.getTransaction().commit();

        //To check if key is generated.
          String keyString=KeyFactory.keyToString(book.getBookId());


        //TESTED OK:Check if book is inserted properly..
        User uCheck=mgr.find(User.class, userRegistrationNumber);
        Book b uBooks=(ArrayList<Book>)uCheck.getBooks()get(0);

        //TESTED OK:Testing if able to get owner from book entity...
        User owner=b.getUser();

        return book;

    }catch(Exception e){
        mgr.getTransaction().rollback();
        return null;
    }
    finally {

        mgr.close();
    }

}

如何在客户端中接收插入的Book实体?

用户实体类:

@Entity
public class User {
@Id
private String id;
//private String registeredNumber;

private  String userName;
private int sexId;
private String userAddress;
private String pinNumber;
private int ratingCount;
private int reportCount;

private String emailId;
private String interestAreaSubjectId;
private String sharingPolicy;

@OneToMany(targetEntity=com.example.bookemon.Book.class,
        mappedBy="owner",cascade=CascadeType.ALL)
private ArrayList<Book> books;

 //getters and setters...


}

预订实体类:

  @Entity
 public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key bookId;


private String bookName;
private String bookDescription;
private int categoryId;
private String  publicationYear;
private int reportCount;
private int languageId;
private int ratingCount;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="OWNER_ID")
public User owner;

     ////Getters and setters......

}

1 个答案:

答案 0 :(得分:0)

错误信息非常清楚。当您分离对象(关闭PM?)时,对象中没有该字段,因此,如果以后需要它,则应在关闭PM之前加载它。