在使用自动生成的Classendpoint插入方法插入实体时引发Nullpointerexception [英] Nullpointerexception throws when inserting entity using Auto-generated Classendpoint insert method

查看:122
本文介绍了在使用自动生成的Classendpoint插入方法插入实体时引发Nullpointerexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑使用自动生成的端点类。我想使用生成的端点将新对象插入数据存储区。但是,抛出异常。

  fooEndpoint.insertFoo(foo); //引发空指针异常

我的实体类与此源的给定示例类似: https://developers.google.com/appengine/docs/java/datastore/jpa/overview。



这是我的实体:

  @Entity 
公共类Foo {
@Id
@GeneratedValue = GenerationType.IDENTITY)
私钥ID;

以下是堆栈跟踪:

 java.lang.NullPointerException 
at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:318)
at org.datanucleus.api.jpa。 JPAEntityManager.find(JPAEntityManager.java:256)
at com.FooEndpoint.containsFoo(FooEndpoint.java:150)
at com.FooEndpoint.insertFoo(FooEndpoint.java:96)

另一方面,当我使用EntityManager persist方法时,我可以插入新对象。因为,这不会检查是否存在于数据存储区中。



我期望classEndpoint插入方法应该保存对象并将自动密钥分配给ID字段。

或者我需要初始化ID字段。



这是自动生成的端点类insertFoo方法。

  / ** 
*这会将新实体插入到App Engine数据存储中。如果数据存储中已存在实体
*,则会引发异常。
*它使用HTTP POST方法。
*
* @param foo要插入的实体。
* @return插入的实体。
* /
public Foo insertFoo(Foo foo){
EntityManager mgr = getEntityManager();
尝试{
if(containsFoo(foo)){
throw new EntityExistsException(Object already exists);
}
mgr.persist(foo);
} finally {
mgr.close();
}
return foo;
}

这是containsFoo方法

  private boolean containsFoo(Foo foo){
EntityManager mgr = getEntityManager();
boolean contains = true;
try {
Foo item = mgr.find(Foo.class,foo.getID()); //异常发生在这里
if(item == null){
contains = false;
}
} finally {
mgr.close();
}
返回包含;
}

foo.getID()为null。因为,这是新的对象。我期待着,应用引擎为它创建了一个关键。或者我需要为它明确创建一个键?



Foo类中的其他字段是简单类型,如String和布尔值。



感谢您的时间。

解决方案

我有完全相同的问题。
我将介绍我的工作方式。



原始自动生成的终端类相关代码:

private boolean containsFoo(Foo foo){
EntityManager mgr = getEntityManager();
boolean contains = true;
try {
Foo item = mgr.find(Foo.class,foo.getID());
if(item == null){
contains = false;
}
} finally {
mgr.close();
}
返回包含;

$ / code>

更改相关代码以包含对作为传递的实体对象进行空检查一个参数。
$ b

  private boolean containsFoo(Foo foo){
EntityManager mgr = getEntityManager ();
boolean contains = true;
尝试{
//如果未设置ID,则该实体尚不存在。
if(foo.getID()== null)
return false;
Foo item = mgr.find(Foo.class,foo.getID());
if(item == null){
contains = false;
}
} finally {
mgr.close();
}
返回包含;
}

通过这种方式,它可以按照设想工作,尽管我相信更有经验的人答案和解释将会出现。


I am confused to using auto-generated endpoint class. I want to use generated endpoint to insert new object into datastore. But, an exception is throwing.

fooEndpoint.insertFoo(foo); // throws null pointer exception 

My entity class is similar with the given example at this source: https://developers.google.com/appengine/docs/java/datastore/jpa/overview.

Here is my entity:

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

Here is the stack trace:

java.lang.NullPointerException
at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:318)
at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:256)
at com.FooEndpoint.containsFoo(FooEndpoint.java:150)
at com.FooEndpoint.insertFoo(FooEndpoint.java:96)

On the other side, I can insert new object when I use the EntityManager persist method. Because, this does not check exist or not on the datastore.

I expect that, classEndpoint insert method should save the object and assing auto key to ID field.

Or I need to initialize the ID field.

Here is auto-generated endpoint class insertFoo method.

  /**
 * This inserts a new entity into App Engine datastore. If the entity already
 * exists in the datastore, an exception is thrown.
 * It uses HTTP POST method.
 *
 * @param foo the entity to be inserted.
 * @return The inserted entity.
 */
public Foo insertFoo(Foo foo) {
    EntityManager mgr = getEntityManager();
    try {
        if (containsFoo(foo)) {
            throw new EntityExistsException("Object already exists");
        }
        mgr.persist(foo);
    } finally {
        mgr.close();
    }
    return foo;
}

Here is the containsFoo method

    private boolean containsFoo(Foo foo) {
    EntityManager mgr = getEntityManager();
    boolean contains = true;
    try {
        Foo item = mgr.find(Foo.class, foo.getID());  // exception occurs here
        if (item == null) {
            contains = false;
        }
    } finally {
        mgr.close();
    }
    return contains;
}

foo.getID() is null. Because, it is new object. I am expecting that, app engine creates a key for it. Or I need to explicitly create a key for it?

Other fields in Foo class are simple types such as String and booleans.

Thanks for your time.

解决方案

I had exactly the same problem. I will present the way I worked around it.

Original auto-generated Endpoints class relevant code:

private boolean containsFoo(Foo foo) {
    EntityManager mgr = getEntityManager(); 
    boolean contains = true;
    try {
        Foo item = mgr.find(Foo.class, foo.getID());
        if (item == null) {
            contains = false;
        }
    } finally {
        mgr.close();
    }
    return contains;
}

Changed relevant code to include a null check for the entity object that is passed as an argument.

private boolean containsFoo(Foo foo) {
    EntityManager mgr = getEntityManager(); 
    boolean contains = true;
    try {
        // If no ID was set, the entity doesn't exist yet.
        if(foo.getID() == null)
            return false;
        Foo item = mgr.find(Foo.class, foo.getID());
        if (item == null) {
            contains = false;
        }
    } finally {
        mgr.close();
    }
    return contains;
}

This way it will work as supposed, although I'm confident that more experienced answers and explanations will appear.

这篇关于在使用自动生成的Classendpoint插入方法插入实体时引发Nullpointerexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆