集合映射的问题

时间:2017-09-10 12:55:41

标签: java hibernate caching

我遇到了映射集合缓存的奇怪问题。

现在我有两个班级:

@Entity
@Table(name = "mods", catalog = "artfunpw")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class Mod implements java.io.Serializable {
***
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "mods") 
    @OrderBy("ReleaseDate desc")
    @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
    public Set<ModVersion> getModsVersionses() {
        return this.modsVersionses;
    }
***

@Entity
@Table(name = "mods_versions", catalog = "artfunpw", uniqueConstraints = @UniqueConstraint(columnNames = "VersionNumber"))
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class ModVersion implements java.io.Serializable {

我的Hibernate缓存配置是

    props.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
    props.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
    props.setProperty("hibernate.cache.use_second_level_cache", "true");
    props.setProperty("hibernate.cache.use_query_cache", "true");
    factoryBean.setHibernateProperties(props);

第一次运行我的选择代码时

Session s = sessionFactory.getCurrentSession();
        List<ModVersion> modVersions  = s.createQuery("select m.modsVersionses from Mod m")
                .setCacheable(true) 
                .list();

我收藏了

[arthur.khusnutdinov.mysitev2.pub.mods.db.ModVersion@4063fbec, arthur.khusnutdinov.mysitev2.pub.mods.db.ModVersion@7a0d599e, arthur.khusnutdinov.mysitev2.pub.mods.db.ModVersion@48087e35]

但是在第二次通话时,我收集了空值:

[null, null, null]

我做错了什么以及如何防止空值而不是预期的对象?

非常感谢!

1 个答案:

答案 0 :(得分:0)

奇怪,但在我从

重新编写查询后,它已修复
select m.modsVersionses from Mod m

select o from Mod m join m.modsVersionses o 

但是我不知道,为什么它发生了以及为什么它用第二个查询缓存,因为它们都被转换为相同的SQL查询

 select
        modsversio1_.id as id1_2_,
        modsversio1_.BuiltWithGameVersion as BuiltWit2_2_,
        modsversio1_.Description as Descript3_2_,
        modsversio1_.DownloadCount as Download4_2_,
        modsversio1_.DownloadCountExeFile as Download5_2_,
        modsversio1_.DownloadUrl as Download6_2_,
        modsversio1_.DownloadUrlExeFile as Download7_2_,
        modsversio1_.modId as modId10_2_,
        modsversio1_.ReleaseDate as ReleaseD8_2_,
        modsversio1_.VersionNumber as VersionN9_2_ 
    from
        artfunpw.mods mod0_ 
    inner join
        artfunpw.mods_versions modsversio1_ 
            on mod0_.id=modsversio1_.modId