我创建了一个与数据库表对应的类,并且这样保存。该类用于存储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应该删除指定为参数的标记条目。
我在代码中尝试了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就会成功创建并且我可以持久化,从数据库中查找和删除条目没有问题。任何帮助或指针将不胜感激。
答案 0 :(得分:0)
它没有直接回答你的问题,但是jpa的实体管理器内置了基于id删除实体的功能,使用了entityManager.find()和entityManager.remove(),所以不需要命名查询
答案 1 :(得分:0)
而不是
"DELETE FROM ApnsToken tok WHERE tok.Token = ?1"
试试
"DELETE FROM ApnsToken tok WHERE tok.Token = :someVariable"