java.lang.NullPointerException当我尝试使用transaction-type =" JTA"创建EntityManager时

时间:2015-12-25 07:09:37

标签: java hibernate jpa

我需要在持久性单元中使用transaction-type="JTA"类型

我有这个配置:

<persistence-unit name="COLIBRI" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>shiro.entety.User</class>
        <class>avaya.entety.AvayaSdr</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="hibernate.connection.url" value="jdbc:sqlserver://....;DatabaseName=sp"/>
            <property name="hibernate.connection.username" value="..."/>
            <property name="hibernate.connection.password" value="..."/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.use_sql_comments" value="true" />
        </properties>
    </persistence-unit>
</persistence>

我有这个课程:

import org.hibernate.Session;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.List;

public interface BaseDao<E> {

    E persist(E e);

    E get(Long id);

    List<E> getAll();

    E merge(E e);

    E refresh(E e);

    void remove(E e);

    void remove(Long id);

    void saveAll(List<E> entityList);

    void flush();

    Session getSession();
    EntityTransaction getEntityTransaction();
    EntityManager getEntityManager();
}

public abstract class BaseJpaDao<E> implements BaseDao<E> {

    protected Class<?> entityClass;

    @PersistenceContext(unitName = "COLIBRI")
    private EntityManager em;

    public BaseJpaDao(Class<?> entityClass) {
        this.entityClass = entityClass;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public E persist(E e) {
        em.persist(e);
        return e;
    }

    @Override
    public E get(Long id) {
        return (E) em.find(entityClass, id);
    }

...

之后我创建了Dao界面:

public interface AvayaDao extends BaseDao<AvayaSdr> {

    List<AvayaSdr> getAll();
    void insertCdr(AvayaSdr avayaSdr);
}

和DaoJpa:

public class AvayaJpaDao extends BaseJpaDao<AvayaSdr> implements AvayaDao {
private static AvayaJpaDao instance;
private final Logger logger = Logger.getLogger(getClass().getName());

public AvayaJpaDao() {
    super(AvayaSdr.class);
}

@Override
public List<AvayaSdr> getAll() {
    return super.getAll();
}


public synchronized static AvayaJpaDao me() {
    if (instance == null) {
        instance = new AvayaJpaDao();
    }
    return instance;
}


public void insertCdr(AvayaSdr avayaCmCdr) {
    persist(avayaCmCdr);
}

}

但是当我打电话时

List<AvayaSdr> resultList = AvayaJpaDao.me().getAll();

我有一个错误:

java.lang.NullPointerException

我找到了如何使用JTA this

我试过这样做:

@Stateless
public class AvayaJpaDao extends BaseJpaDao<AvayaSdr> implements AvayaDao {

但在我的public abstract class BaseJpaDao<E> implements BaseDao<E> {

突出显示错误BaseJpaDao 我不打算如何使用注释创建JTAEntityManager

0 个答案:

没有答案