Reputation: 920
I have created a class corresponding to a database table and is persisted as such. The class is used to store APNS tokens (Not that it matters to the question) and is as follow:
@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() {
}
}
The persistence.xml is as follow:
<?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>
Now, my problem is that when I am deploying the EAR, the EntityManager fails to be created, and I know it is the NamedQuery that causes the problem because when I comment it out everything works fine... Now I am pretty convinced that my mistake is a stupid one, but I have been searching over the web to see examples and everything looks fine to me.
The NamedQuery is supposed to delete entries with the Token specified as parameter.
I have tried both the NamedQuery in the code and one like this:
DELETE FROM ApnsToken tok WHERE tok.Token = :mTok
Here's the stacktrace:
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)
Like I said, if I remove the NamedQuery the EntityManager is created successfully and I can persist, find and remove entries from the database without a problem. Any help or pointer will be greatly appreciated.
Upvotes: 1
Views: 771
Reputation: 9477
Instead of
"DELETE FROM ApnsToken tok WHERE tok.Token = ?1"
try
"DELETE FROM ApnsToken tok WHERE tok.Token = :someVariable"
Upvotes: 0
Reputation: 889
It doesn't answer your question directly, but jpa's entity manager has built in functionality to remove an entity based on an id, by using entityManager.find() and entityManager.remove(), so no need for a named query.
Upvotes: 1