我在這個問題上被阻塞了好幾個小時,並且我不明白這個問題,因爲它可以與我的項目的另一個實體正常工作。 我在Eclipse IDE中用Glassfish 4,JPA 2和EJB 3創建了一個Java EE項目。 我正在使用mysql數據庫進行存儲。EJB未知abstrat模式,但它存在於數據庫中
我創建了一個名爲Company的實體bean和一個DAO來管理它。我做了所有需要的工作,並且運作良好。 問題是我爲另一個實體bean做了完全相同的事情,名爲Newsletter,但是這個不起作用。 Java拋出一個EJBException,告訴我抽象模式是未知的,但它存在於數據庫中。 表公司和通訊是在相同的數據庫,所以我用他們兩個相同的持久性單位。
我在Google或這裏找到的一些問題是通過將該類添加到persitence-unit中來解決的,但是它仍然引發了一個異常。
這裏是我的通訊表:
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| idNewsletter | int(11) | NO | PRI | NULL | auto_increment |
| firstname | varchar(100) | NO | | NULL | |
| lastname | varchar(100) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
我的豆通訊:
@Entity
public class Newsletter {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idNewsletter;
@Column(name="firstname")
private String firstname;
@Column(name="lastname")
private String lastname;
@Column(name="email")
private String email;
}
這裏是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="2lifedb_PU" transaction-type="JTA">
<jta-data-source>jdbc/bonecp_resource</jta-data-source>
<class>com.twolife.beans.User</class>
<class>com.twolife.beans.Company</class>
<class>com.twolife.beans.Office</class>
<class>com.twolife.beans.BankDetails</class>
<class>com.twolife.beans.Logo</class>
<class>com.twolife.beans.Newsletter</class>
<properties>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
而DAO類:
@Stateless
public class DAONewsletter {
private static final String SQL_SELECT_NEWSLETTER = "SELECT n FROM NEWSLETTER n WHERE n.email = :email";
@PersistenceContext(unitName="2lifedb_PU")
private EntityManager em;
public void create(Newsletter newsletter) throws DAOException {
try {
em.persist(newsletter);
} catch (Exception e) {
throw new DAOException(e);
}
}
public Newsletter find(String email) throws DAOException {
Newsletter newsletter = new Newsletter();
Query query = em.createQuery(SQL_SELECT_NEWSLETTER);
query.setParameter("email", email);
try {
newsletter = (Newsletter) query.getSingleResult();
} catch (NoResultException e) {
return null;
} catch (Exception e) {
throw new DAOException(e);
}
return newsletter;
}
}
最後,這裏是我的異常堆棧跟蹤:
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Problem compiling [SELECT n FROM NEWSLETTER n WHERE n.email = :email].
[14, 24] The abstract schema type 'NEWSLETTER' is unknown.
[33, 40] The state field path 'n.email' cannot be resolved to a valid type.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1585)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:456)
at com.twolife.dao.DAONewsletter.find(DAONewsletter.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
... 36 more
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Problem compiling [SELECT n FROM NEWSLETTER n WHERE n.email = :email].
[14, 24] The abstract schema type 'NEWSLETTER' is unknown.
[33, 40] The state field path 'n.email' cannot be resolved to a valid type.
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:347)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1583)
... 60 more
如果anoyone得到一個想法,那將是非常有益的! 對不起,我不是母語...
太感謝你了!我不知道一個實體管理器使用JPQL語法,沒有SQL語法。這就解決了! –