我需要将我的数据库记录缓存到我的应用程序中,并在整个应用程序中使用它。我不明白如何使我的实体类可缓存。在某处,我读到我们需要提交以使实体类可缓存,但在我的情况下,我在数据库中预先存储了配置数据,这可能很少改变,我们不会从我们的类中持久化。
我发布了我的代码,我试图达到上述要求。
这是我的bean类:
@Entity(name="ActCodeMetadataBean")
@Table(name="ACT_CODE_MTDT")
@Cacheable(true)
public class ActCodeMetadataBean {
@Id
private String ACT_CODE;
private String PRDT_ID;
private String ACT_ACTN;
private String ACT_EXEC_ORDR;
private String RQST_STRC_ID;
@Temporal(TemporalType.DATE)
private Date CRTN_DATE;
@Temporal(TemporalType.DATE)
private Date LAST_UPDT_DATE;
public ActCodeMetadataBean() {
super();
// TODO Auto-generated constructor stub
}
public String getACT_CODE() {
return ACT_CODE;
}
public void setACT_CODE(String aCT_CODE) {
ACT_CODE = aCT_CODE;
}
public String getPRDT_ID() {
return PRDT_ID;
}
public void setPRDT_ID(String pRDT_ID) {
PRDT_ID = pRDT_ID;
}
public String getACT_ACTN() {
return ACT_ACTN;
}
public void setACT_ACTN(String aCT_ACTN) {
ACT_ACTN = aCT_ACTN;
}
public String getACT_EXEC_ORDR() {
return ACT_EXEC_ORDR;
}
public void setACT_EXEC_ORDR(String aCT_EXEC_ORDR) {
ACT_EXEC_ORDR = aCT_EXEC_ORDR;
}
public String getRQST_STRC_ID() {
return RQST_STRC_ID;
}
public void setRQST_STRC_ID(String rQST_STRC_ID) {
RQST_STRC_ID = rQST_STRC_ID;
}
public Date getCRTN_DATE() {
return CRTN_DATE;
}
public void setCRTN_DATE(Date cRTN_DATE) {
CRTN_DATE = cRTN_DATE;
}
public Date getLAST_UPDT_DATE() {
return LAST_UPDT_DATE;
}
public void setLAST_UPDT_DATE(Date lAST_UPDT_DATE) {
LAST_UPDT_DATE = lAST_UPDT_DATE;
}
public ActCodeMetadataBean(String aCT_CODE, String pRDT_ID,
String aCT_ACTN, String aCT_EXEC_ORDR, String rQST_STRC_ID,
Date cRTN_DATE, Date lAST_UPDT_DATE) {
super();
ACT_CODE = aCT_CODE;
PRDT_ID = pRDT_ID;
ACT_ACTN = aCT_ACTN;
ACT_EXEC_ORDR = aCT_EXEC_ORDR;
RQST_STRC_ID = rQST_STRC_ID;
CRTN_DATE = cRTN_DATE;
LAST_UPDT_DATE = lAST_UPDT_DATE;
}
}
我的persistance.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="OMSDemo" transaction-type="RESOURCE_LOCAL" >
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.airtel.oms.entity.ProvisioningInterface</class>
<class>com.airtel.oms.entity.ProvisioningTask</class>
<!-- Cache classes -->
<class>com.airtel.cache.ActCodeMetadataBean</class>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orclaq"/>
<property name="javax.persistence.jdbc.user" value="artl"/>
<property name="javax.persistence.jdbc.password" value="artl"/>
<property name="javax.persistence.sharedCache.mode" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
这是主要的:
class Main {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("OMSDemo");
EntityManager manager = factory.createEntityManager();
manager.setProperty("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
Query query = (Query) manager.createNativeQuery("select * from ACT_CODE_MTDT", ActCodeMetadataBean.class);
query.setHint("javax.persistence.cache.storeMode",
CacheStoreMode.USE);
ActCodeMetadataBean account=null;
List<Object> results = query.getResultList();
for(Object e : results)
{
System.out.println(e.toString());
}
Cache cache = factory.getCache();
// cache.
System.out.println("cache.contain should return true: "+cache.contains(ActCodeMetadataBean.class, account.getACT_CODE()));
cache.evict(ActCodeMetadataBean.class);
System.out.println("cache.contain should return false: "+cache.contains(ActCodeMetadataBean.class, account.getACT_CODE()));
manager.close();
factory.close();
}
} : Here is the orcle table which i need to cache
以下是回复:
cache.contain should return true: false
cache.contain should return false: false
我能做些什么来实现它?