Hibernate中将Annotation转化为*.hbm.xml的核心实现

代码实现了:你给一个javabean类加上Annotation注释,可以将其转化为*.hbm.xml的格式打印出来,如果想转化为文件的形式也不难,用流就行了。

主要用到了自定义Annotation。


Persistent.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)	//表示这个Annotation可以修饰类,接口或者枚举定义
@Retention(RetentionPolicy.SOURCE)      //表示只是在源码中保留,不可在运行期间使用到
@Documented			//指定被该元素修饰的Annotation类将被 javadoc 工具提取成文档,若是定义Annotation类时使用到了@Documented修饰,则
				//所有使用该Annotation的程序的API文档中将含该Annotation说明	
public @interface Persistent
{
	String table();
}
Id.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)	//表示这个Annotation可以修饰成员变量
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface Id
{
	String generator();
	String type();
	String column();
}
Property.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
@Documented

public @interface Property
{
	String column();
	String type();
}

Person.java // 这就相当于javabean,不过没有写get \ set 方法,这不是重点

@Persistent(table="person_table")
class Person
{
	@Id(column="person_id",type="integer" ,generator="identity")
	private int id;
	@Property(column="person_name" ,type="string")
	private String name;
	@Property(column="person_age" ,type="integer")
	private int age;

}

HibernateAnnotationProcessor.java

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;

@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes({"Persistent","Id","Property"})

public class HibernateAnnotationProcessor extends AbstractProcessor
{
	public boolean process(Set<? extends TypeElement>annotations,RoundEnvironment roundEnv)
	{
		//遍历所有被 @Persistent 修饰的类
		for(Element e:roundEnv.getElementsAnnotatedWith(Persistent.class))   
		{
			Name clazzName=e.getSimpleName();
			Persistent per	= e.getAnnotation(Persistent.class);
			System.out.println("正在处理"+clazzName);
			System.out.println("<class name="+e+" table="+per.table()+">");

			//遍历所有类中Field区域
			for(Element fi : e.getEnclosedElements())
			{
				//只处理Field上的Annotation
				if(fi.getKind() == ElementKind.FIELD)
				{
					//获取当前Field上的@Id Annotation
					Id id = fi.getAnnotation(Id.class);
					if(id!=null)
					{
						System.out.println("<id name="+fi.getSimpleName()+" column="+id.column()+" type="+id.type()+">");
						System.out.println("<generator class="+id.generator()+"/>");
						System.out.println("</id>");	
					}
					//获取Field定义上的 @Property Annotation
 					Property p=fi.getAnnotation(Property.class);
					if(p!=null)
					{
						System.out.println("<property name="+fi.getSimpleName()+" column="+p.column()+"  type="+p.type()+">");
																							
					}
				}
				
			}
			System.out.println("</class>");
				
		}
		return true;
		
	}

}



先编译 HibernateAnnotationProcessor.java 
然后 javac -processor HibernateAnnotationProcessor Person.java







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值