亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于Spring的CrudRepository返回Entity的問題

關于Spring的CrudRepository返回Entity的問題

函數式編程 2019-03-12 13:15:59
各位高手,我在使用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<String> users;}2.repositorypublic interface GroupRepository extends CrudRepository<Group, UUID>{}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;    } 但我使用groupRepository.findOne方法時,users返回的是org.hibernate.collection.internal.PersistentBag類型,而不是ArrayList類型,請問是什么原因?
查看完整描述

1 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

這個PersistentBag也是List的一種實現。Hibernate為啥不直接用ArrayList呢,因為它要支持懶加載,所以需要在類似ArrayList實現的基礎上再包裝一層。比如你調用List的get、size之類的方法,如果是懶加載,這個PersistentBag會先去把數據從數據庫里面讀進來,再做操作。如果是Eager的(就像你的代碼設置的),那么就和一般的ArrayList沒啥區別了。

對于用戶來說,是沒什么感受的,你就把他當List用就好。


查看完整回答
反對 回復 2019-04-24
  • 1 回答
  • 0 關注
  • 601 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號