继承Model,以Key-name Value-class保存Entity,统一保存在ApplicationContext中
Entity主要是保存@Entity的生命周期方法以及Entity的名称.
package org.jboss.seam;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.persistence.EmbeddedId;
import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.init.EjbDescriptor;
import org.jboss.seam.init.EjbEntityDescriptor;
import org.jboss.seam.util.Reflections;
public class Entity extends Model
{
private Method preRemoveMethod;
private Method prePersistMethod;
private Method preUpdateMethod;
private Method postLoadMethod;
private Method identifierGetter;
private Field identifierField;
private Method versionGetter;
private Field versionField;
private String name;
/**
*
* @param beanClass
*
* Use Entity.forBean() or Entity.forClass
*/
@Deprecated
public Entity(Class<?> beanClass)
{
super(beanClass);
EjbDescriptor descriptor = Seam.getEjbDescriptor(beanClass);
if (descriptor instanceof EjbEntityDescriptor)
{
mergeAnnotationAndOrmXml((EjbEntityDescriptor) descriptor);
}
else
{
mergeAnnotationAndOrmXml(null);
}
}
public Method getPostLoadMethod()
{
return postLoadMethod;
}
public Method getPrePersistMethod()
{
return prePersistMethod;
}
public Method getPreRemoveMethod()
{
return preRemoveMethod;
}
public Method getPreUpdateMethod()
{
return preUpdateMethod;
}
@Deprecated
public Field getIdentifierField()
{
return identifierField;
}
@Deprecated
public Method getIdentifierGetter()
{
return identifierGetter;
}
@Deprecated
public Field getVersionField()
{
return versionField;
}
@Deprecated
public Method getVersionGetter()
{
return versionGetter;
}
public Object getIdentifier(Object entity)
{
if (identifierGetter != null)
{
return Reflections.invokeAndWrap(identifierGetter, entity);
}
else if (identifierField != null)
{
return Reflections.getAndWrap(identifierField, entity);
}
else
{
throw new IllegalStateException("@Id attribute not found for entity class: " + getBeanClass().getName());
}
}
public Object getVersion(Object entity)
{
if (versionGetter != null)
{
return Reflections.invokeAndWrap(versionGetter, entity);
}
else if (versionField != null)
{
return Reflections.getAndWrap(versionField, entity);
}
else
{
return null;
}
}
public String getName()
{
return name;
}
public static Entity forBean(Object bean)
{
return forClass(bean.getClass());
}
public static Entity forClass(Class clazz)
{
if (!Contexts.isApplicationContextActive())
{
throw new IllegalStateException("No application context active");
}
Class entityClass = Seam.getEntityClass(clazz);
//检查clazz是JPA Entity or Ejb 实体部署描述符
/**
* public static Class getEntityClass(Class clazz)
{
while (clazz != null && !Object.class.equals(clazz))
{
if (clazz.isAnnotationPresent(Entity.class))
{
return clazz;
}
else
{
EjbDescriptor ejbDescriptor = Seam.getEjbDescriptor(clazz);
if (ejbDescriptor != null)
{
return ejbDescriptor.getBeanType() == ComponentType.ENTITY_BEAN ? clazz : null;
}
else
{
clazz = clazz.getSuperclass();
}
}
}
return null;
}
*/
if (entityClass == null)
{
throw new NotEntityException("Not an entity class: " + clazz.getName());
}
String name = getModelName(entityClass); //调用Super
Model model = (Model) Contexts.getApplicationContext().get(name);//所有的Model都存放在ApplicationContext全局中
if (model == null || !(model instanceof Entity))
{
Entity entity = new Entity(entityClass);
Contexts.getApplicationContext().set(name, entity);
return entity;
}
else
{
return (Entity) model;
}
}
private void mergeAnnotationAndOrmXml(EjbEntityDescriptor descriptor)
{
// Lookup the name of the Entity from XML, annotation or default
this.name = lookupName(getBeanClass(), descriptor);
if (this.name == null)
{
throw new NotEntityException("Unable to establish name of entity " + getBeanClass());
}
if (descriptor != null)
{
// Set any methods and fields we need metadata for from the XML
// descriptor. These take priority over annotations
this.preRemoveMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPreRemoveMethodName());
this.prePersistMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPrePersistMethodName());
this.preUpdateMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPreUpdateMethodName());
this.postLoadMethod = getEntityCallbackMethod(getBeanClass(), descriptor.getPostLoadMethodName());
this.identifierField = descriptor.getIdentifierFieldName() != null ? Reflections.getField(getBeanClass(), descriptor.getIdentifierFieldName()) : null;
this.identifierGetter = descriptor.getIdentifierPropertyName() != null ? Reflections.getGetterMethod(getBeanClass(), descriptor.getIdentifierPropertyName()) : null;
this.versionField = descriptor.getVersionFieldName() != null ? Reflections.getField(getBeanClass(), descriptor.getVersionFieldName()) : null;
this.versionGetter = descriptor.getVersionPropertyName() != null ? Reflections.getGetterMethod(getBeanClass(), descriptor.getVersionPropertyName()) : null;
}
if (descriptor == null || !descriptor.isMetaDataComplete())
{
for ( Class<?> clazz=getBeanClass(); clazz!=Object.class; clazz = clazz.getSuperclass() )
{
for ( Method method: clazz.getDeclaredMethods() )
{
//TODO: does the spec allow multiple lifecycle method
// in the entity class heirarchy?
if (this.preRemoveMethod == null && method.isAnnotationPresent(PreRemove.class))
{
this.preRemoveMethod = method;
}
if (this.prePersistMethod == null && method.isAnnotationPresent(PrePersist.class) )
{
this.prePersistMethod = method;
}
if (preUpdateMethod == null && method.isAnnotationPresent(PreUpdate.class) )
{
preUpdateMethod = method;
}
if (postLoadMethod == null && method.isAnnotationPresent(PostLoad.class) )
{
postLoadMethod = method;
}
if (identifierField == null && identifierGetter == null && method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(EmbeddedId.class))
{
identifierGetter = method;
}
if (versionField == null && versionGetter == null && method.isAnnotationPresent(Version.class) )
{
versionGetter = method;
}
}
if ( ( identifierGetter == null && identifierField == null ) || ( versionField == null && versionGetter == null ) )
{
for ( Field field: clazz.getDeclaredFields() )
{
if ( identifierGetter == null && identifierField == null && (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)))
{
identifierField = field;
}
if ( versionGetter == null && versionField == null && field.isAnnotationPresent(Version.class) )
{
versionField = field;
}
}
}
}
}
setAccessible(this.preRemoveMethod);
setAccessible(this.prePersistMethod);
setAccessible(this.preUpdateMethod);
setAccessible(this.postLoadMethod);
setAccessible(this.identifierField);
setAccessible(this.identifierGetter);
setAccessible(this.versionField);
setAccessible(this.versionGetter);
}
private void setAccessible(AccessibleObject accessibleObject)
{
if (accessibleObject != null)
{
accessibleObject.setAccessible(true);
}
}
/**
* 查找@Entity(name="?")是否设置Name属性
*/
private static String lookupName(Class<?> beanClass, EjbEntityDescriptor descriptor)
{
if (descriptor != null && descriptor.getEjbName() != null)
{
// XML overrides annotations
return descriptor.getEjbName();
}
else if ( (descriptor == null || !descriptor.isMetaDataComplete()) && beanClass.isAnnotationPresent(javax.persistence.Entity.class) && !"".equals(beanClass.getAnnotation(javax.persistence.Entity.class).name()))
{
// Is a name specified?
return beanClass.getAnnotation(javax.persistence.Entity.class).name();
}
else if (descriptor != null || beanClass.isAnnotationPresent(javax.persistence.Entity.class))
{
// Use the default name if either a descriptor is specified or the
// annotation is present
return beanClass.getName();
}
else
{
return null;
}
}
/**
* 调用Entity的方法,可以使任何方法,此方法应该是用来方便调用生命周期方法的
*/
private static Method getEntityCallbackMethod(Class beanClass, String callbackMethodName)
{
try
{
if (callbackMethodName != null)
{ //根据方法名获取方法
return Reflections.getMethod(beanClass, callbackMethodName);
}
else
{
return null;
}
}
catch (IllegalArgumentException e)
{
throw new IllegalArgumentException("Unable to find Entity callback method specified in orm.xml", e);
}
}
public static class NotEntityException extends IllegalArgumentException
{
public NotEntityException(String string)
{
super(string);
}
}
}
分享到:
相关推荐
在Spring MVC框架中,HttpEntity和ResponseEntity是两个非常重要的概念,它们主要用于处理HTTP请求和响应。本项目“springMVC-HttpEntity(ResponseEntity)demo”是一个实战演示,展示了如何在Spring MVC应用中使用...
《深入理解System.Data.Entity》 System.Data.Entity是.NET框架中一个关键的部分,它构成了Entity Framework的核心,这是一个强大的对象关系映射(ORM)框架,用于简化数据库操作。ORM允许开发人员使用面向对象的...
TAC KBP Chinese Entity Linking Comprehensive Training and Evaluation Data 2011-2014 LDC2015E17 March 20, 2015 Linguistic Data Consortium 1. Overview Text Analysis Conference (TAC) is a series...
### Entity Framework 4 In Action:全面解析与应用实践 #### 一、书籍概述与背景介绍 《Entity Framework 4 In Action》是一本深入探讨Entity Framework 4(简称EF4)的权威指南,由Stefano Mostarda、Marco De ...
在本篇详细知识点讲解中,将基于给定文件信息,深入探讨Entity Framework(实体框架)中Code First方法的相关知识点。根据文件标题《Programming Entity Framework DbContext》和描述,该文件应该是关于Entity ...
Entity Developer是一个强大的ORM设计器,支持 ADO.NET Entity Framework, NHibernate, LinqConnect 和 LINQ to SQL。你可以使用模型首先和数据首先的方法设计ORM模型并生成C#或者Visual Basic .NET代码。它引入了新...
EntityFrameworkCore是一个强大的ORM(对象关系映射)框架,专为.NET Core和.NET Framework设计,由微软维护。它使得.NET开发者无需直接操作SQL语句,就能通过C#代码与数据库进行交互,极大地提高了开发效率。Entity...
Entity Framework 4.0和*** Web Forms是微软公司推出的用于构建Web应用程序的技术,这本书主要讲述了如何使用Entity Framework 4.0在*** Web Forms应用程序中实现数据的显示和编辑。 Entity Framework是微软的.NET...
Entity Framework(简称EF)是一个微软的ORM(对象关系映射)框架,它允许开发人员通过面向对象的方式来操作数据库,而不是直接使用SQL语句。EF是作为.NET Framework的一部分提供的,并且在.NET4.0版本中得到了微软...
Entity Framework(EF)是微软提供的一个对象关系映射(O/RM)框架,它简化了.NET开发人员访问数据库的代码编写,无需手动编写大量数据访问代码。EF允许开发者通过.NET对象模型来操作数据库。Entity Framework Core...
### ADO.NET Entity Framework 教程知识点概览 #### 一、Entity Framework 概述 - **背景**:Entity Framework (EF) 是 Microsoft 推出的一款 ORM (Object Relational Mapping) 工具,旨在简化数据访问层的开发,...
在Android开发中,由于HttpClient库的稳定性和强大的功能,即使在Android API级别33及更高版本中不再内置,许多开发者依然选择使用Apache HttpClient,包括`org.apache.http.entity.mime`包中的类,来进行网络通信和...
Entity Framework 多表查询方式 Entity Framework 是一个强大的数据访问技术,提供了多种查询方式来满足不同的业务需求。在本节中,我们将详细介绍 Entity Framework 中的多表查询方式,包括简单查询、查询部分字段...
Entity Framework(EF)是微软提供的一款强大的对象关系映射(ORM)框架,它允许开发者使用.NET语言(如C#或VB.NET)来操作数据库,而无需编写大量的SQL语句。这个压缩包“entityFramework源代码”包含的是Entity ...
Entity Framework (EF) 是微软提供的一款强大的对象关系映射(ORM)框架,它允许开发者使用.NET语言(如C#或VB.NET)来操作数据库,而无需编写大量的SQL语句。在.NET开发中,EF极大地提高了开发效率,因为它将数据...
Entity Developer是一个强大的ORM设计器,支持 ADO.NET Entity Framework, NHibernate, LinqConnect 和 LINQ to SQL。你可以使用模型首先和数据首先的方法设计ORM模型并生成C#或者Visual Basic .NET代码。它引入了新...
**Z.EntityFramework.Extensions注册机** 在IT行业中,Entity Framework(EF)是.NET框架下的一款非常流行的对象关系映射(ORM)工具,它允许开发者使用面向对象的编程方式来操作数据库,而无需关注底层的SQL语句。...
MySQL.Data.Entity.6.10.9 和 MySQL.Data.6.10.9 是两个针对MySQL数据库操作的重要组件,主要用于.NET Framework环境中的Entity Framework(EF)集成。在本篇文章中,我们将深入探讨这两个库以及它们如何协同工作,...
《深入理解Z.EntityFramework.Extensions.EFCore6.13.1:多版本支持与BulkInsert功能》 Z.EntityFramework.Extensions是一款针对Entity Framework Core(简称EF Core)的扩展库,它提供了丰富的功能,增强了数据库...