我创建了一个与数据库表相对应的类,并因此保持不变。该类用于存储APNS令牌(与问题无关),如下所示:
@Entity
@Table(name = "ApnsToken")
@NamedQuery(name = "apnsToken.removeByToken", query = "DELETE FROM ApnsToken tok WHERE tok.Token = ?1")
public class ApnsToken implements Serializable {
@Transient
private static final long serialVersionUID = 1516719311585221856L;
@TableGenerator(name = "ApnsTokenGen", table = "Sequences", pkColumnName = "BeanName", valueColumnName = "SeqNumber", initialValue = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "ApnsTokenGen")
@Column(name = "TokenID", nullable = false)
private Long tokenId;
@Column(name = "Token")
private String Token;
public ApnsToken() {
}
}
persistence.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="unit-name" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>data-source</jta-data-source>
<class>com.package.model.ApnsToken</class>
<properties>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereTransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
现在,我的问题是,当我部署EAR时,EntityManager无法创建,并且我知道是NamedQuery导致了该问题,因为当我将其注释掉时,一切都很好...现在,我非常确信我的错误是一个愚蠢的错误,但是我一直在网上搜索示例,一切对我来说都很好。
NamedQuery应该删除指定了Token作为参数的条目。
我已经尝试了代码中的NamedQuery和这样的代码:
DELETE FROM ApnsToken tok WHERE tok.Token = :mTok
这是堆栈跟踪:
javax.ejb.EJBException: Injection failure; nested exception is: java.lang.IllegalStateException: EntityManagerFactory has not been created for PU : PuId=myapp-ear#common-1.0.1-WS-websphere.jar#myapp-repos
Caused by: java.lang.IllegalStateException: EntityManagerFactory has not been created for PU : PuId=myapp-ear#common-1.0.1-WS-websphere.jar#myapp-repos
at com.ibm.ws.jpa.management.JPAPUnitInfo.getEntityManagerFactory(JPAPUnitInfo.java:1369)
at com.ibm.ws.jpa.management.JPAPUnitInfo.getEntityManagerPool(JPAPUnitInfo.java:1577)
at com.ibm.ws.jpa.management.JPATxEntityManager.<init>(JPATxEntityManager.java:156)
at com.ibm.ws.jpa.management.JPAComponentImpl.getEntityManager(JPAComponentImpl.java:1053)
at com.ibm.ws.util.JPAJndiLookupObjectFactory.getObjectInstance(JPAJndiLookupObjectFactory.java:151)
at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectionObject(InjectionBinding.java:659)
at com.ibm.wsspi.injectionengine.InjectionTargetField.inject(InjectionTargetField.java:245)
at com.ibm.ws.injectionengine.InjectionEngineImpl.inject(InjectionEngineImpl.java:620)
at com.ibm.ejs.container.StatelessBeanO.initialize(StatelessBeanO.java:287)
at com.ibm.ejs.container.CMStatelessBeanOFactory.create(CMStatelessBeanOFactory.java:45)
at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome.java:1031)
at com.ibm.ejs.container.EJSHome.createBeanO(EJSHome.java:1141)
at com.ibm.ejs.container.activator.UncachedActivationStrategy.atActivate(UncachedActivationStrategy.java:84)
at com.ibm.ejs.container.activator.Activator.activateBean(Activator.java:599)
at com.ibm.ejs.container.EJSContainer.preInvokeActivate(EJSContainer.java:3964)
at com.ibm.ejs.container.EJSContainer.EjbPreInvoke(EJSContainer.java:3349)
at com.package.repos.EJSLocal1SLRepositoryDao_3de24cf0.putApnsToken(EJSLocal1SLRepositoryDao_3de24cf0.java)
就像我说的,如果删除NamedQuery,则EntityManager创建成功,并且可以持久存在,从数据库中查找和删除条目没有问题。任何帮助或指针将不胜感激。
最佳答案
它不会直接回答您的问题,但是jpa的实体管理器具有内置功能,可以通过使用EntityManager.find()和entityManager.remove()来基于ID删除实体,因此不需要命名查询。
关于java - NamedQuery导致EntityManager创建失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22251720/