代码之家  ›  专栏  ›  技术社区  ›  Innet

QuartzJob中的EntityManager为空

  •  1
  • Innet  · 技术社区  · 7 年前

    我在QuartzJob实例上使用EntityManager时遇到了这个问题:

    Java语言lang.NullPointerException 在weblogic。坚持不懈CICScopedEMProvider。getEMForCurrentCIC(CICScopedEMProvider.java:35) 在weblogic。坚持不懈事务实体管理器Proxy Impl。getPersistenceContext(TransactionalEntityManagerProxy-Impl.java:122) 在weblogic。坚持不懈BasePersistenceContextProxyImpl。调用(BasePersistenceContextProxyImpl.java:94) 在weblogic。坚持不懈事务实体管理器Proxy Impl。调用(TransactionalEntityManagerProxy-Impl.java:164) 在weblogic。坚持不懈BasePersistenceContextProxyImpl。调用(BasePersistenceContextProxyImpl.java:86) 在com。太阳代理$Proxy321.createQuery(未知源) 在com。我的项目。存储库。impl。光沉积IMPL。getShotsFromBackupOption(PhotopositoryImpl.java:44) 在com。我的项目。服务impl。PhotoServiceImpl。getPhoto(PhotoServiceImpl.java:90) 在com。我的项目。班长备份作业。执行(BackupJob.java:46) 在org。石英果心JobRunShell。运行(JobRunShell.java:202) 在org。石英简单。SimpleThreadPool$WorkerThread。运行(SimpleThreadPool.java:573)

    这是我的工作:

    public class BackupJob implements Job{
        private Logger logger = LoggerFactory.getLogger(getClass());
    
        @Inject private PhotoService photoService;
    
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            try {
                String jobName = context.getJobDetail().getKey().getName();
                Long jobGroup = Long.parseLong(context.getJobDetail().getKey().getGroup());
    
                List<File> fileList = photoService.getPhoto(jobName, jobGroup);
    
                logger.info("Backup with deep {}", context.getJobDetail().getJobDataMap().get("deep"));
            } catch (Exception e) {
                logger.error("ERROR during backup job", e);
            }       
        }
    }
    

    (photoService被正确注入,这与我在photoService中调用的photoRepository方法相同)

    这是我的存储库:

    @Stateless
    public class PhotoRepositoryImpl implements PhotoRepository{
    
        @PersistenceContext(unitName=Constant.PU_NAME)
        private EntityManager em;
    
        @Override
        public List<Photo> getShotsFromBackupOption(String jobName, Long jobGroup) throws SQLException{
    
            Query query = em.createQuery("SELECT p "
                                        + "FROM Shot s "
                                        + "JOIN Photo p "
                                        + "WHERE s.dateShooting < "
                                            + "(SELECT b.previousThan "
                                            + "FROM BackupOption b "
                                            + "WHERE b.jobName = :jobName AND b.syncType.id = :jobGroup)"
                                        + "ORDER BY p.relativePath DESC");
            query.setParameter("jobName", jobName);
            query.setParameter("jobGroup", jobGroup);
    
    
            return (List<Photo>) query.getResultList();
        }
    }
    

    这是我的工作工厂

    public class JobFactoryImpl implements JobFactory {
    
    
    @Inject
    private Instance<Job> jobs;
    
    @Override
    public Job newJob(TriggerFiredBundle triggerFiredBundle, Scheduler scheduler) throws SchedulerException {
        final JobDetail jobDetail = triggerFiredBundle.getJobDetail();
        final Class<? extends Job> jobClass = jobDetail.getJobClass();
    
        for (Job job : jobs) {
            if (job.getClass().isAssignableFrom(jobClass)) {
                return job;
            }
        }
    
        throw new RuntimeException("Cannot create a Job of type " + jobClass);
    }}
    

    NullPointerException在em.createquery(---)方法上引发。 当方法从我的QuartzJob中调用时,这个查询工作得很好。 你有什么好的解决方法吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Innet    7 年前

    我通过以下方式解决了获取EntityManager的问题:

    @PersistenceUnit(unitName= Constant.PU_NAME)
    private EntityManagerFactory emf;
    
    private EntityManager em = emf.createEntityManager();