Nutz DAO lazy loading entities associated with the object
Advertisements
1. Define a method called LinkFieldGetterIntercepter interceptor: the getter for the call to POJO automatically load related objects.
2. From the definition of a EntityHolder: reloadEntity entity created when the proxy class, add the definition in Article 1 of the interceptor. And using entity mappings in the cache proxy class in the entity.
3. From the definition of a org.nutz.dao.entity.Borning: easy when the ResultSet into a POJO, POJO to create the proxy class instance, not the original POJO class entity.
4. Since the definition of a EntityMaker: make analytical method entity setBorning (...) is defined in section 3 Borning object.
Here I use JPA as the POJO's annotation, extended the relevant code, if used directly Nutz annotation should be easier to achieve. Method is the same.
1. LinkFieldGetterIntercepter:
public class LinkFieldGetterIntercepter extends AbstractMethodInterceptor {
private Dao dao;
private Link link;
public LinkFieldGetterIntercepter(Dao dao, Link link) {
this.dao = dao;
this.link = link;
}
public Object afterInvoke(Object obj, Object returnObj, Method method, Object... args) {
// You can determine whether to returnObj null, To determine whether it needs to load the associated entity, after loading the associated entities writeback obj The associated fields
if (link.isOne()) {
......
} else if (link.isMany()) {
......
} else if (link.isManyMany()) {
......
}
Mirrors.setValue(obj, link.getOwnField().getName(), returnObj);
return returnObj;
}
}
2. EntityHolder
public class DemsyEntityHolder {
private Dao dao;// Dao Object
private EntityMaker maker;
private Map<Class<?>, Entity<?>> mappings;// <AOP The proxy class, an entity >
private Map<Class, Class> agentClassMap;// < The entity classes, AOP proxy class >
public DemsyEntityHolder(DemsyNutDao dao) {
this.dao = dao;
this.maker = dao.getEntityMaker();
mappings = new HashMap<Class<?>, Entity<?>>();
agentClassMap = new HashMap<Class, Class>();
}
public <T> Entity<T> getEntity(Class<T> classOfT) {
// Converted to AOP proxy class
if (!Mirrors.isAgent(classOfT)) {
classOfT = agentClassMap.get(classOfT);
}
if (classOfT == null) {
return null;
}
return (Entity<T>) mappings.get(classOfT);
}
@SuppressWarnings("unchecked")
public <T> Entity<T> reloadEntity(Class<T> classOfT, boolean autoCreateTable) {
JPAEntity<?> entity = (JPAEntity<?>) maker.make(null, null, classOfT);
// Cached entity
mappings.put(agentClass(entity), entity);
return (Entity<T>) entity;
}
// Create a proxy class
private Class agentClass(JPAEntity entity) {
Class cls = entity.getType();
if (Mirrors.isAgent(cls)) {
return cls;
}
ClassAgent classAgent = new AsmClassAgent();
List<Link> links = entity.getLinks(null);
if (links != null) {
for (Link link : links) {
String fieldName = link.getOwnField().getName();
fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
classAgent.addInterceptor(MethodMatcherFactory.matcher("get" + fieldName),
new LinkFieldGetterIntercepter(dao, link));
}
}
Class agentClass = classAgent.define(new DefaultClassDefiner(), cls);
entity.setAgentMirror(Mirror.me(agentClass));
this.agentClassMap.put(cls, agentClass);
return agentClass;
}
public int count() {
return mappings.size();
}
}
3 Borning:
class DemsyBorning implements Borning {
JPAEntity entity;
DemsyBorning(JPAEntity entity) {
this.entity = entity;
}
/**
* Create an instance of the entity agent
*
* @return
* @throws Exception
*/
public Object create() throws Exception {
// Create an instance of the proxy class, instead of the original instance of the class
return entity.getAgentMirror().born();
}
public Object born(ResultSet rs, FieldMatcher fm) throws Exception {
Object obj = create();
Iterator<EntityField> it = entity.fields().iterator();
while (it.hasNext()) {
EntityField ef = it.next();
if (null == fm || fm.match(ef.getField().getName()))
ef.fillValue(obj, rs);
}
return obj;
}
}
4 EntityMaker:
......
public Entity<?> make(DatabaseMeta db, Connection conn, Class<?> type) {
......
// Borning
entity.setBorning(new DemsyBorning(entity));
......
Related Posts of Nutz DAO lazy loading entities associated with the object
-
Development of commonly used open source J2EE project
Read other people's records, feel good, on the collection here, Original Source: http://blog.csdn.net/fenixshadow/archive/2007/11/17/1890010.aspx Persistence Layer 1: 1) Hibernate This need not introduce, and used very frequently, used more are mapped
-
ajax input prompt implementation
Projects have used at the time entered by the user to give some tips, suggesting that the contents are extracted from the database (in Chinese). So a google search under the amended part of their code to share with you. Attachment is available at the ...
-
Hibernate access picture sample
General web users to upload picture at treatment would normally uses two types of strategies: First, put directly into the picture in the database Blob field; II database are stored only at the picture on the path of the server information?, Pictures stor
-
[Have resolved] how to give rails of the model class of congestive (include / extent module)
Has been resolved Please visit: http://henrik.nyh.se/2008/02/rails-model-extensions 1: the introduction of new features to the controller My approach: I tried to introduce in the application controller extra modules: require "string" This "string
-
hibernate load delay
Lazy loading: Lazy loading mechanism is in order to avoid unnecessary performance overhead and put forward the so-called lazy loading is required when the real data at a time when the real implementation of the data load operation. At Hibernate provides f
-
Hibernate Inteceptor
The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...
-
EJB
public transient int counter; / / transient property private String firstname; / / persistent property @ Transient String getLengthInMeter () (...) / / transient property String getName () (...) / / persistent property @ Basic int getLength () (...) / / p
-
JDBC example of a long time do not have JDBC forgot
A back-up here to stay. The first: The second:
-
Application of spring struts2.0 hibernate HQL
Therefore, in the development of statistical inquiry system, as far as possible through the use of select statement to write the required query property way back relational data, and avoid using the first query return persistent object (in this way are in
-
In the servlet use Bean
According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...












