Eclipselink EntityManager为同一实体返回不同的实例

时间:2015-06-17 10:16:39

标签: java spring jpa eclipselink

我在spring mvc应用程序中使用EclipseLink JPA。根据文件 About Persisting Objects应该返回一个实例:

@AncestorInPath

在我的应用中,a1和a2不是同一个实例。为什么会这样?

配置如下:

final Account a1 = entityManager.find(Account.class, 2851L);
final Account a2 = entityManager.find(Account.class, 2851L);
assert a1 == a2;

persistence.xml几乎为空:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackageClasses = Application.class)
@EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
class JpaConfig implements TransactionManagementConfigurer {
    private static final Logger LOGGER = LoggerFactory.getLogger(JpaConfig.class);

    @Autowired
    protected DataSource dataSource;

    @Bean(name="entityManagerFactory")
    @DependsOn({"flyway"})
    public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() {
        LOGGER.debug("Initialize EntityManagerFactory");
        final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setPackagesToScan("some.package");
        entityManagerFactoryBean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());
        entityManagerFactoryBean.setJpaProperties(getJPAProperties());
        entityManagerFactoryBean.afterPropertiesSet();
        return entityManagerFactoryBean;
    }

    protected Properties getJPAProperties() {
        final Properties jpaProperties = new Properties();
        jpaProperties.setProperty(PersistenceUnitProperties.WEAVING, "static");
        jpaProperties.setProperty(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.NONE);
        jpaProperties.setProperty(PersistenceUnitProperties.LOGGING_LOGGER, "some.package.Slf4jSessionLogger");
        jpaProperties.setProperty(PersistenceUnitProperties.LOGGING_LEVEL, "FINE");
        jpaProperties.setProperty(PersistenceUnitProperties.LOGGING_PARAMETERS, "true");
        jpaProperties.setProperty(PersistenceUnitProperties.LOGGING_TIMESTAMP, "true");
        jpaProperties.setProperty(PersistenceUnitProperties.LOGGING_SESSION, "true");
        return jpaProperties;
    }

    @Bean(name="transactionManager")
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new JpaTransactionManager();
    }
}

0 个答案:

没有答案