1

I have been trying to cache an entity in hibernate hazelcast. This entity is dependent on another entity. I have tried the mapping in the following ways. The first one caches after first em.find but the second one doesn't.

@Entity
@Table(name = "config")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "hibernate-average-config")
public class Config implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "foreignIdGenerator")
    @GenericGenerator(
            name = "foreignIdGenerator", strategy = "foreign",
            parameters = @Parameter(name = "property", value = "provider"))
    private int id;

    @Fetch(SELECT)
    @OneToOne(optional = false)
    @JoinColumn(name = "prov_id", nullable = false, updatable = false, unique = true)
    private Provider provider;

This above snippet works but the following does not

@Entity
@Table(name = "config")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "hibernate-average-config")
public class Config implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @MapsId
    @Fetch(SELECT)
    @OneToOne(optional = false)
    @JoinColumn(name = "prov_id", unique = true, nullable = false, updatable = false)
    private Provider provider;

Why is it like that? Is there any way to have effective cache on the second one so that i don't have to keep repetitive column in my table.

Rakib
  • 145
  • 13
  • Do you have stacktrace? What kind of exception do you get? – Mesut Dec 17 '19 at 10:35
  • No exception. Mapping is fine. Second snippet makes a select query on each em.find(Config.class, id) which means it is not reading values from cache where the first one makes the query only once and reads value from cache for later calls. – Rakib Dec 17 '19 at 11:07
  • I think your issue may be related to mine. https://stackoverflow.com/questions/61238000/hibernate-second-level-cache-doesnt-work-for-onetoone-associations – Heits Jul 30 '20 at 20:06

0 Answers0