GAE/J - JPA 错误 - 类型(“”)不是实体的类型,但需要用于此操作

发布于 2024-12-14 07:54:42 字数 1509 浏览 3 评论 0原文

我对 JPA 不太熟悉,但我做了一些搜索,但找不到这个错误代码,所以我将其发布在这里。

我正在尝试保留此类:

@Entity(name = "UserBasket")
public class UserBasket extends BaseBasket implements Serializable  {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long Id;

    private static final long serialVersionUID = 1L;

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }


}

使用此方法调用:

public Long createUserBasket(UserBasket basket) {
try{
        synchronized (this) {
            EntityManager em = EMFService.get().createEntityManager();
            em.persist(basket);
            em.close();
        }
    }catch(Exception ex){

        //log.severe("Uh oh!" + ex.toString());
    }
}

并收到此错误:

java.lang.IllegalArgumentException: Type ("") is not that of an entity but needs to be for this operation

我在 GAE 上运行此代码。我怀疑这与我的实体有关,但我不确定是什么。

编辑:填写更多详细信息 -

这是 BaseBasket (我删除了 getter 和 setter)

@Entity(name = "BaseBasket")
public class BaseBasket {

    public String basketID;
        public List<BasketItem> items;

}

我用一个简单的方法创建了 UserBasket:

UserBaset b = new UserBasket();

然后分配各种值。

我没有使用 datanucleus 增强器,这只是我天真的理解,它不是必需的,并且由于这些对象(UserBasket 等)在我的应用程序的客户端和服务器部分之间共享,我想让它们保持简单。

I'm a little green with JPA but I did some searching and couldn't find this error code so I'll post it here.

I'm trying to persist this class:

@Entity(name = "UserBasket")
public class UserBasket extends BaseBasket implements Serializable  {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long Id;

    private static final long serialVersionUID = 1L;

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }


}

With this method call:

public Long createUserBasket(UserBasket basket) {
try{
        synchronized (this) {
            EntityManager em = EMFService.get().createEntityManager();
            em.persist(basket);
            em.close();
        }
    }catch(Exception ex){

        //log.severe("Uh oh!" + ex.toString());
    }
}

And getting this Error:

java.lang.IllegalArgumentException: Type ("") is not that of an entity but needs to be for this operation

I'm running this on GAE. I suspect it's something to do with my Entity but I'm not sure what.

Edit: Filling in more details -

Here is BaseBasket (I cut out the getters and setters)

@Entity(name = "BaseBasket")
public class BaseBasket {

    public String basketID;
        public List<BasketItem> items;

}

And I create the UserBasket with a simple:

UserBaset b = new UserBasket();

And then assign the various values.

I didn't use the datanucleus enhancer as, and this is only my naive understanding, that it isn't required and as these objects (UserBasket etc) are shared between the client and server part of my application I wanted to keep them simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人如初 2024-12-21 07:54:42

由于您使用的是 JPA,请检查 persistence.xml 定义是否符合 DataNucleus 文档

特别是,如果您不使用映射文件,请检查您想要保留的所有实体是否已在类元素中声明。作为提供的示例:

<!-- Online Store -->
<persistence-unit name="OnlineStore">
    <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
    <class>org.datanucleus.samples.metadata.store.Product</class>
    <class>org.datanucleus.samples.metadata.store.Book</class>
    <class>org.datanucleus.samples.metadata.store.CompactDisc</class>
    <class>org.datanucleus.samples.metadata.store.Customer</class>
    <class>org.datanucleus.samples.metadata.store.Supplier</class>
    <exclude-unlisted-classes/>
    <properties>
        <property name="datanucleus.ConnectionDriverName" value="org.h2.Driver"/>
        <property name="datanucleus.ConnectionURL" value="jdbc:h2:datanucleus"/>
        <property name="datanucleus.ConnectionUserName" value="sa"/>
        <property name="datanucleus.ConnectionPassword" value=""/>
    </properties>
</persistence-unit>

Since you're using JPA, check that your persistence.xml definition is conform to the DataNucleus documentation

In particular, if you're not using a mapping file, check that all your entities you want to persist are declared in classes elements. As the provided example :

<!-- Online Store -->
<persistence-unit name="OnlineStore">
    <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
    <class>org.datanucleus.samples.metadata.store.Product</class>
    <class>org.datanucleus.samples.metadata.store.Book</class>
    <class>org.datanucleus.samples.metadata.store.CompactDisc</class>
    <class>org.datanucleus.samples.metadata.store.Customer</class>
    <class>org.datanucleus.samples.metadata.store.Supplier</class>
    <exclude-unlisted-classes/>
    <properties>
        <property name="datanucleus.ConnectionDriverName" value="org.h2.Driver"/>
        <property name="datanucleus.ConnectionURL" value="jdbc:h2:datanucleus"/>
        <property name="datanucleus.ConnectionUserName" value="sa"/>
        <property name="datanucleus.ConnectionPassword" value=""/>
    </properties>
</persistence-unit>
一曲爱恨情仇 2024-12-21 07:54:42

DataNucleus中,此错误也可能是由于作用于null的实体而引起的,例如:

MyEntity entity = txn.em().find(MyEntity.class, entityId); // no such record, returns 'null'
txn.em().lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT); // IllegalArgumentException: Type ("") ...

In DataNucleus, this error can also be caused by acting on an entity which is null, e.g.:

MyEntity entity = txn.em().find(MyEntity.class, entityId); // no such record, returns 'null'
txn.em().lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT); // IllegalArgumentException: Type ("") ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文