2014-03-07 72 views
1

我已經創建了一個對應於數據庫表的類,並且保持原樣。類是用來存儲APNS令牌(這不是問題的問題),是如下:NamedQuery導致EntityManager創建失敗

@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

它並不直接回答你的問題,但jpa的實體管理器已經內置了基於id的實體去除功能,通過使用entityManager.find()和entityManager.remove(),所以不需要命名查詢。

+0

的問題是我不會直接訪問ID,只有條目中包含的標記。這就是爲什麼我需要NamedQuery – Gladhus

+0

好吧,試試這個「刪除tok從ApnsToken tok WHERE tok.Token =:id」我想你可能需要'tok'之間刪除和從 –

+0

好吧,所以我只是嘗試了這一點,它沒有完全相同的東西和之前:/ – Gladhus

0

而不是

"DELETE FROM ApnsToken tok WHERE tok.Token = ?1" 

嘗試

"DELETE FROM ApnsToken tok WHERE tok.Token = :someVariable" 
+0

我已經嘗試過,沒有任何成功,我會將其添加到我的問題。 – Gladhus