emreturka
emreturka

Reputation: 876

javax.ejb.EJBTransactionRolledbackException glassfish 3.1 with EJB

I have create Entity,DAo and Façade in a Web app. I have no error on my codes but I am getting this exception while using find(T.class,id) method of JPA.An also it says that there is no @Entity annotation on my Entity. But this is not true.How to solve this problem.

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>

Stacktrace;

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)

WARNING: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean at com.sun.ejb.containers.BaseContainer.checkExceptionClientTx(BaseContainer.java:5071) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4906) at 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 SEVERE: Caused by: javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean SEVERE: at com.sun.ejb.containers.BaseContainer.checkExceptionClientTx(BaseContainer.java:5071) SEVERE: at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4906) SEVERE: at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) SEVERE: ... 71 more SEVERE: Caused by: java.lang.IllegalArgumentException: Unknown entity bean class: class com.mesutemre.businesModel.Uyeler, please verify that this class has been marked with the @Entity annotation. SEVERE: at org.eclipse.persistence.internal.jpa.EntityManagerImpl.find(EntityManagerImpl.java:648) SEVERE:

Upvotes: 0

Views: 4713

Answers (1)

unwichtich
unwichtich

Reputation: 13857

It looks like this is a redeployment/caching problem with GlassFish. If you use transaction type RESOURCE_LOCAL and create the EntityManager manually and redeploy, it may be the case that the old EntityManager or the according factory is still cached by GlassFish and therefore only knows some old values.

The fastest solution should be a restart of GlassFish and redeployment of the application.

Another solution is closing the EntityManagerFactory explicitly on un/redeployment.

Anyway the preferred solution is to use transaction type JTA so the EntityManager gets managed by the container. This is a lot easier to use and less error-prone. Here is a short example:

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);
    }
}

See also:

Upvotes: 2

Related Questions