我正在嘗試Spring Boot和Hibernate Second Level Caching。我成功地實現了它的工作。然而,如果ID列是一個VARCHAR,我不能從第二級緩存中獲取實體。下面是相關的類:如果ID列是VARCHAR,Hibernate不會從第二級緩存中獲取
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "user1")
@Entity
public class User {
private long userId;
private String name;
@Id
@Column(name = "id")
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String name) {
this.name = name;
}
public User() {
}
}
這個實體工作得很好,這裏有從休眠狀態的調試日誌第一和第二取
取1
o.h.e.t.internal.TransactionImpl : begin
org.hibernate.SQL : select user0_.id as id1_1_0_, user0_.name as name2_1_0_ from user1 user0_ where user0_.id=?
o.h.l.p.e.p.i.ResultSetProcessorImpl : Starting ResultSet row #0
l.p.e.p.i.EntityReferenceInitializerImpl : On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified
o.h.engine.internal.TwoPhaseLoad : Resolving associations for [com.voyant.master.entities.User#1]
o.h.engine.internal.TwoPhaseLoad : Adding entity to second-level cache: [com.voyant.master.entities.User#1]
o.h.engine.internal.TwoPhaseLoad : Done materializing entity [com.voyant.master.entities.User#1]
o.h.r.j.i.ResourceRegistryStandardImpl : HHH000387: ResultSet's statement was not registered
.l.e.p.AbstractLoadPlanBasedEntityLoader : Done entity load : com.voyant.master.entities.User#1
o.h.e.t.internal.TransactionImpl : committing
取2
o.h.e.t.internal.TransactionImpl : begin
o.h.e.t.internal.TransactionImpl : committing
現在,有問題的實體定義:
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Table(name = "user")
@Entity
public class User2 {
@Id
@Column(name = "username")
private String username;
@Column(name = "lastname")
private String lastname;
@Column(name = "name")
private String name;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User2(String name) {
this.name = name;
}
public User2() {
}
}
休眠從同一實體
的兩個連續取調試日誌取1
o.h.e.t.internal.TransactionImpl : begin
o.h.jpa.criteria.CriteriaQueryImpl : Rendered criteria query -> select generatedAlias0 from User2 as generatedAlias0 where generatedAlias0.username=:param0
org.hibernate.SQL : select user2x0_.username as username1_0_, user2x0_.lastname as lastname2_0_, user2x0_.name as name3_0_ from user user2x0_ where user2x0_.username=?
org.hibernate.loader.Loader : Result set row: 0
org.hibernate.loader.Loader : Result row: EntityKey[com.voyant.master.entities.User2#dharam]
o.h.engine.internal.TwoPhaseLoad : Resolving associations for [com.voyant.master.entities.User2#dharam]
o.h.engine.internal.TwoPhaseLoad : Adding entity to second-level cache: [com.voyant.master.entities.User2#dharam]
o.h.engine.internal.TwoPhaseLoad : Done materializing entity [com.voyant.master.entities.User2#dharam]
o.h.e.t.internal.TransactionImpl : committing
o.h.e.i.AbstractFlushingEventListener : Processing flush-time cascades
o.h.e.i.AbstractFlushingEventListener : Dirty checking collections
o.h.e.i.AbstractFlushingEventListener : Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
o.h.e.i.AbstractFlushingEventListener : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
o.hibernate.internal.util.EntityPrinter : Listing entities:
o.hibernate.internal.util.EntityPrinter : com.voyant.master.entities.User2{name=dharam, username=dharam, lastname=dharam}
取2
o.h.e.t.internal.TransactionImpl : begin
o.h.jpa.criteria.CriteriaQueryImpl : Rendered criteria query -> select generatedAlias0 from User2 as generatedAlias0 where generatedAlias0.username=:param0
org.hibernate.SQL : select user2x0_.username as username1_0_, user2x0_.lastname as lastname2_0_, user2x0_.name as name3_0_ from user user2x0_ where user2x0_.username=?
org.hibernate.loader.Loader : Result set row: 0
org.hibernate.loader.Loader : Result row: EntityKey[com.voyant.master.entities.User2#dharam]
o.h.engine.internal.TwoPhaseLoad : Resolving associations for [com.voyant.master.entities.User2#dharam]
o.h.engine.internal.TwoPhaseLoad : Adding entity to second-level cache: [com.voyant.master.entities.User2#dharam]
o.h.engine.internal.TwoPhaseLoad : Done materializing entity [com.voyant.master.entities.User2#dharam]
o.h.e.t.internal.TransactionImpl : committing
o.h.e.i.AbstractFlushingEventListener : Processing flush-time cascades
o.h.e.i.AbstractFlushingEventListener : Dirty checking collections
o.h.e.i.AbstractFlushingEventListener : Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
o.h.e.i.AbstractFlushingEventListener : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
o.hibernate.internal.util.EntityPrinter : Listing entities:
o.hibernate.internal.util.EntityPrinter : com.voyant.master.entities.User2{name=dharam, username=dharam, lastname=dharam}
可以請你改變你的用戶類工具可串行化,看看它是否工作。 ********* public class User實現Serializable { – user8271644
抱歉!用戶類都很好。問題與User2類,但是!我會嘗試你的建議。 – dharam
道歉我指的是有問題的人。 – user8271644