java - 关于Spring的CrudRepository返回Entity的问题
大家讲道理
大家讲道理 2017-04-18 09:51:04
0
1
391

各位高手,我在使用hibernate、jpa、springdata集成时遇到了如下的问题:

1.model

@Entity @Table(name="group",schema="sc") @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Group extends AbstractEntity { ... @ElementCollection(fetch= FetchType.EAGER) @NotNull @Column(name="user") @CollectionTable(name = "t_group_users", schema = "sc", joinColumns = @JoinColumn(name = "group_id")) private List users; }

2.repository

public interface GroupRepository extends CrudRepository { }

3.config

@Configuration @EnableJpaRepositories(basePackages = { "com.**.**" }) public class RepositoryTestConfig { @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean(); emfb.setDataSource(dataSource()); emfb.setJpaVendorAdapter(jpaVendorAdapter()); emfb.setPackagesToScan("com.**.**.model"); Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "create"); properties.setProperty("hibernate.format_sql", "true"); emfb.setJpaProperties(properties); return emfb; } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setDatabase(Database.H2); adapter.setShowSql(false); adapter.setGenerateDdl(false); adapter.setDatabasePlatform("org.hibernate.dialect.H2Dialect"); return adapter; } @Bean(name = "testFunctionDataSource") public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:~/test;INIT=CREATE SCHEMA IF NOT EXISTS sc"); dataSource.setUsername("sa"); dataSource.setPassword(""); return dataSource; } }

但我使用groupRepository.findOne方法时,users返回的是org.hibernate.collection.internal.PersistentBag类型,而不是ArrayList类型,请问是什么原因?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all (1)
巴扎黑

This PersistentBag is also an implementation of List. Why doesn't Hibernate use ArrayList directly? Because it needs to support lazy loading, it needs to be wrapped with another layer based on a similar ArrayList implementation. For example, if you call methods such as get and size of List, if it is lazy loading, the PersistentBag will first read the data from the database and then perform operations. If it is Eager (as set by your code), then it is no different from a normal ArrayList.

For users, there is no feeling, just use it as a List.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!