【问题标题】:Spring Entities Not Saved To DatabaseSpring 实体未保存到数据库
【发布时间】:2015-10-30 10:34:37
【问题描述】:

我们已经这样设置了 Spring 框架:

@Eager
public interface CatalogElementRepository extends PagingAndSortingRepository<CatalogElementEntity, Long> {

}

@Service
public class CatalogImpl implements CatalogManager {

    @Inject
    CatalogElementRepository catalogElementRepository;

    @Override
    public CatalogElement createCatalogElement(CatalogElementEntity catalogElement) {
    return this.catalogElementRepository.save(catalogElement);
    }
}

@Stateless
@Remote(CatalogManager.class)
public class CatalogManagerBean implements CatalogManager {

    @Inject
    CatalogManager delegate;

    @Override
    public CatalogElement createCatalogElement(CatalogElementEntity catalogElement) {
        return this.delegate.createCatalogElement(catalogElement);
    }
}

因此,每当有人调用远程接口createCatalogElement 上的方法时,我都会假设实体已存储在数据库中。它没有(奇怪的是,findOne 仍然返回相同的实体,但无法通过findByProperty 找到它)。

Otherquestions 说要添加@Transactional,所以为了安全起见,我在方法和类上添加了@javax.transaction.Transactionalorg.springframework.transaction.annotation.Transactional,没有任何效果。

可能是什么问题?

我没有看到 Spring Framework 的任何配置文件,但它是一个遗留项目,所以它们可能被隐藏得很好。

【问题讨论】:

    标签: java spring jpa spring-data-jpa


    【解决方案1】:

    出于某种原因,将此类用作EntityManager 的生产者有助于:

    public class SpringConfig {
    
    @PersistenceUnit
    EntityManagerFactory emf;
    
    @PersistenceContext
    EntityManager em;
    
    @Produces
    @ApplicationScoped
    public EntityManagerFactory createEntityManagerFactory() {
        return this.emf;
    }
    
    @Produces
    public EntityManager createEntityManager() {
        return this.em;
    }
    
    public void close(@Disposes EntityManagerFactory entityManagerFactory) {
        entityManagerFactory.close();
    }
    
    public void close(@Disposes EntityManager entityManager) {
        entityManager.close();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      相关资源
      最近更新 更多