实体管理器/持久性文件结构 [英] Entity Manager / persistance file structure

查看:109
本文介绍了实体管理器/持久性文件结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我想不出一个标题,标题不清楚,但是,我有一个EJB项目,正在尝试使用JPA.要创建实体管理器,我要通过注释

The title isn't clear as I couldn't think of one but, I have an EJB project and am trying to play with JPA. To create the entity manager I am injecting it in via annotations

@PersistenceContext(unitName="testConnection")  
private EntityManager em;

运行我认为可以的测试查询

When I run a test query which I believe to be fine

Query userQuery = em.createQuery("SELECT u FROM TestUser u WHERE u.username = 'test' u.password = 'test'");
tu = (TestUser) userQuery.getSingleResult();

我得到一个异常,指出EJB无法创建实体管理器.

I get an exception which points toward the EJB not being able to create the entity manager.

奇怪的是,当我跑步时

tu = (TestUser) em.find(TestUser.class, id);

工作正常

我的项目结构是

  EAR
    EJB
    EJB Client
    JPA
       persistance.xml

我想这是主要问题

严重:java.lang.IllegalStateException:无法为unitName testConnection检索EntityManagerFactory

SEVERE: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName testConnection

当前使用依赖项链接各个项目.

The individual projects are currently linked using dependencies.

有什么想法吗?

谢谢 乔恩

推荐答案

我有一个EJB项目,正在尝试使用JPA.要创建实体管理器,我要通过注释将其注入

I have an EJB project and am trying to play with JPA. To create the entity manager I am injecting it in via annotation

注释部分本身看起来正确.

The annotation part itself looks correct.

当我运行我认为可以的测试查询时

when I run a test query which i believe to be fine

我认为不是,它至少在WHERE子句中缺少AND.但实际上我会这样写:

I don't think it is, it is at least missing an AND in the WHERE clause. But I would write it like this actually:

Query userQuery = em.createQuery("SELECT u FROM TestUser u WHERE u.username = :name AND u.password = :password");
userQuery.setParameter("name", "test");
userQuery.setParameter("password", "test");
tu = (TestUser) userQuery.getSingleResult();

我得到一个异常,指出EJB无法创建实体管理器.奇怪的是,当我运行(...)时,它运行正常.

I get an exception which points toward the EJB not being able to create the entity manager. The strange thing is that when I run (...) it works fine.

考虑到您得到的例外,这确实很奇怪.

That's strange indeed given the exception that you get.

我的项目结构是(...)

My project structure is (...)

我不知道它是否是错字,但它是persistence.xml,而不是persistance.xml,它应该位于持久性单元根目录的META-INF目录中.

I don't know if it's a typo or not but it's persistence.xml, not persistance.xml, and it should be located in a META-INF directory of the root of the persistence unit.

有什么想法吗?

So any thoughts?

修复查询,修复persistence.xml包装.另外,请提供其内容和完整的堆栈跟踪.

Fix the query, fix the persistence.xml packaging. Also please provide its content and the full stack trace.

这篇关于实体管理器/持久性文件结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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