我在Web应用程序中创建了Entity,DAo和Façade。我的代码没有错误但是在使用JPA的find(T.class,id)方法时我得到了这个异常。它还说我的实体上没有@Entity注释。但事实并非如此。如何解决这个问题。
myEntity所
@Entity
@Table(name = "uyeler")
public class Uyeler implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "kullaniciadi")
private String kullaniciadi;
@Column(name = "sifre")
private String sifre;
@Column(name = "ad")
private String ad;
@Column(name = "soyad")
private String soyad;
@Column(name = "cinsiyet")
private String cinsiyet;
@Column(name = "ilgialanlari")
private String ilgialanlari;
@Column(name = "dogumtarihi")
private String dogumtarihi;
@Column(name = "eposta")
private String eposta;
@Column(name = "epostahaberdar")
private String epostahaberdar;
public String getKullaniciadi() {
return kullaniciadi;
}
MyDaoImpl;
public abstract class UyelerDaoImpl<T> {
private final static String UNIT_NAME ="KutuphaneOtomasyonuEJB";
@PersistenceUnit(unitName = UNIT_NAME)
private EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory(UNIT_NAME);
private EntityManager em = emf.createEntityManager();
public Uyeler findMemberByUserName(String username){
return em.find(Uyeler.class, username);
}
}
的persistence.xml
<persistence-unit name="KutuphaneOtomasyonuEJB"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>jdbc/MySQLConnectionPool</non-jta-data-source>
<class>com.mesutemre.businesModel.Kitaplar</class>
<class>com.mesutemre.businesModel.Uyeler</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="FINEST" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/kutuphane" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
</properties>
</persistence-unit>
堆栈跟踪;
WARNING: EJB5184:A system exception occurred during an invocation on EJB UyelerDAO, method: public com.mesutemre.businesModel.Uyeler com.mesutemre.businesDAOs.UyelerDAO.findMemberByUserName(java.lang.String)
警告:javax.ejb.TransactionRolledbackLocalException:从com.sun.ejb.containers.BaseContainer.checkExceptionClientTx(BaseContainer.java:5071)中的bean抛出异常,位于com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java) :4906)com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045)at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994)at com.sun.ejb.containers。 EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)at at 严重:由以下原因引起:javax.ejb.TransactionRolledbackLocalException:从bean抛出的异常 严重:在com.sun.ejb.containers.BaseContainer.checkExceptionClientTx(BaseContainer.java:5071) 严重:在com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4906) 严重:在com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) 严重:......还有71个 严重:由以下原因引起:java.lang.IllegalArgumentException:未知的实体bean类:类com.mesutemre.businesModel.Uyeler,请验证此类是否已使用@Entity注释进行标记。 严重:在org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:648) 严重:
答案 0 :(得分:2)
看起来这是GlassFish的重新部署/缓存问题。如果您使用事务类型RESOURCE_LOCAL
并手动创建EntityManager
并重新部署,则可能是旧的EntityManager
或相应的工厂仍然由GlassFish缓存,因此只知道一些旧的值。
最快的解决方案应该是重启GlassFish并重新部署应用程序。
另一个解决方案是在un / redeployment上显式关闭EntityManagerFactory。
无论如何,首选解决方案是使用事务类型JTA
,以便EntityManager
由容器管理。这样更容易使用且不易出错。这是一个简短的例子:
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class FileDAO {
@PersistenceContext
private EntityManager em;
public void store(File file) {
em.persist(file);
}
}
另见: