访问Map集合时,Hibernate会加载整个数据库

时间:2015-07-15 22:20:54

标签: hibernate jpa dictionary collections

我有这两个表:

Foo(
  ID INT PK
)

FooDate(
  FOO_ID INT PK,
  DATE DATETIME PK,
  VALUE DECIMAL
)

我有这些用于映射的类:

@Entity
public class Foo implements Serializable {

    @Id
    @Column(name="ID")
    private Integer id;

    @MapKeyTemporal(TemporalType.DATE)
    @MapKey(name = "date")
    @OneToMany(mappedBy = "foo")
    private Map<Date, FooDate> fooDate = new HashMap<>();
}

@Entity
public class FooDate implements Serializable {

    @EmbeddedId
    private FooDateId id;

    @ManyToOne
    @JoinColumn(name = "FOO_ID", insertable = false, updatable = false)     
    private Foo foo;

    @Temporal(TemporalType.DATE)
    @Column(name = "DATE", insertable = false, updatable = false)
    private Date date;

    @Column(name = "VALUE")    
    private BigDecimal value;
}

@Embeddable
public class FooDateId implements Serializable {

    @Column(name = "FOO_ID")
    private Integer foo;

    @Temporal(TemporalType.DATE)
    @Column(name = "DATE")
    private Date date;
}

FooDate有大量数据

当我尝试将新值添加到Map时,Hibernate会加载整个数据库。

当我尝试保存foo实例时会发生同样的情况。

Foo foo = dao.get(1);
foo.fooDate.put(new Date(), new BigDecimal()); //fetch all records

dao.merge(fundo); // same when saving the new value

有没有办法只查询我要插入的Date

1 个答案:

答案 0 :(得分:0)

如果您使用Hibernate,则可以尝试使用特定于Hibernate的extra lazy collections

@MapKeyTemporal(TemporalType.DATE)
@MapKey(name = "date")
@OneToMany(mappedBy = "foo")
@LazyCollection(LazyCollectionOption.EXTRA)
private Map<Date, FooDate> fooDate = new HashMap<>();