科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件Hibernate项目中工具箱指南(3)

Hibernate项目中工具箱指南(3)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

15.2.2. meta属性

作者:中国IT实验室 来源:中国IT实验室 2007年8月21日

关键字:

  • 评论
  • 分享微博
  • 分享邮件

15.2.2. meta属性

    <meta>标签时对hbm.xml文件进行的简单注解,工具可以用这个位置来保存/阅读和Hibernate内核不是直接相关的一些信息。

你可以用<meta>标签来告诉hbm2java只生成"protectd"

下面的例子:

<class name="Person">
<meta attribute="class-description">
Javadoc for the Person class
@author Frodo
</meta>
<meta attribute="implements">IAuditable</meta>
<id name="id" type="long">
<meta attribute="scope-set">protected</meta>
<generator class="increment"/>
</id>
<property name="name" type="string">
<meta attribute="field-description">The name of the person</meta>
</property>
</class>

会生成类似下面的输出(为了有助于理解,节选部分代码)。注意Javadoc注释和声明成protected的set方法:

// default package
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
*         Javadoc for the Person class
*         @author Frodo
*
*/
public class Person implements Serializable, IAuditable {
/** identifier field */
public Long id;
/** nullable persistent field */
public String name;
/** full constructor */
public Person(java.lang.String name) {
this.name = name;
}
/** default constructor */
public Person() {
}
public java.lang.Long getId() {
return this.id;
}
protected void setId(java.lang.Long id) {
this.id = id;
}
/**
* The name of the person
*/
public java.lang.String getName() {
return this.name;
}
public void setName(java.lang.String name) {
this.name = name;
}
}

表 15.6. 支持的meta标签

属性 说明
class-description 插入到类的javadoc说明去
field-description 插入到field/property的javadoc说明去
interface 如果是true,生成interface而非class
implements 类要实现的接口
extends 类要继承的超类(若是subclass,则忽略该属性)
generated-class 重新指定要生成的类名
scope-class class的scope
scope-set set方法的scope
scope-get get方法的scope
scope-field 实际属性字段(field)的scope
use-in-tostring 在toString()中包含此属性
implement-equals 在这个类中包含equals()和hashCode()方法
use-in-equals 在equals()和hashCode() 方法中包含此属性
bound 为属性增加propertyChangeListener支持
constrained 为属性增加vetoChangeListener支持
gen-property 如果是false,不会生成属性(谨慎使用)
property-type 覆盖属性的默认值.如果值是标签,则指定一个具体的类型而非Object(Use this with any tag's to specify the concrete type instead of just Object.)
class-code 在类的最后会插入的额外代码
extra-import 在所有的import后面会插入的额外的import
finder-method 参见下面的"Basic finder generator"
session-method 参见下面的"Basic finder generator"

通过<meta>标签定义的属性在一个hbm.xml文件中是默认"继承"的。

    这究竟是什么意思?如果你希望你所有的类都实现IAuditable接口,那么你只需要加一个<meta attribute="implements">IAuditable</meta> 在你hml.xml文件的开头,就在<hibernate-mapping>后面。现在所有在hbm.xml文件中定义的类都会实现IAuditable了!(除了那些也特别指定了"implements"元属性的类,因为本地指定的元标签总是会覆盖任何继承的元标签)。

    注意,这条规则对所有 的<meta>标签都有效。也就是说它可以用来指定所有的字段都被声明成protected的,而非默认的private。这可以通过在<class>后面<meta attribute="scope-field">protected</meta>指定,那么这个类所有的field都会变成protected。

如果你不想让<meta>标签继承,你可以简单的在标签属性上指明inherit="false",比如<meta attribute="scope-class" inherit="false">public abstract</meta>,这样"class-scope"就只会对当前类起作用,不会对其子类生效。

 

查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章