【问题标题】:Hibernate Annotations Metadata休眠注释元数据
【发布时间】:2011-08-12 11:47:35
【问题描述】:

有没有办法获取关于我的 Hibernate Annotations 的元数据?

我需要关于关联的信息,看看它们是否可为空

现在只能查询ClassMetadata

http://docs.jboss.org/hibernate/core/3.5/javadocs/org/hibernate/metadata/ClassMetadata.html

我可以在哪里运行属性并检查它是否是一个

EntityType
http://docs.jboss.org/hibernate/core/3.5/javadocs/org/hibernate/type/EntityType.html

CollectionType
http://docs.jboss.org/hibernate/core/3.5/javadocs/org/hibernate/type/CollectionType.html

EntityType 显然有一个isNullable 函数但没有CollectionType

所以我想到了使用Annotation信息

 @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "seizureI18n"))
 @Id
 @GeneratedValue(generator = "generator")
 @Column(name = "id", unique = true, nullable = false)
 public Integer getId() {
   return this.id;
 }

这是否可能或另一种方式来实现我想要的。

问候

JS

【问题讨论】:

    标签: java hibernate reflection annotations


    【解决方案1】:

    我不确定我是否理解您的问题,但如果您想查看是否存在注释,您可以这样做(使用自省):

    Annotation[] tabAnnotation = A.class.getField( "fieldB" ).getDeclaredAnnotations( );
    for( Annotation annotation : tabAnnotation )
       if( annotation instanceof Entity )
          System.out.println( ((Entity)annotation).isNullable() );
    

    【讨论】:

    • 谢谢,这正是我要找的东西
    【解决方案2】:

    根据定义,集合不能为空。 n 个元素的集合意味着有 n 个指定类型的实体具有对该实体的外键引用。它并不暗示该实体表中的任何内容。

    因此,您可以检查可空性的唯一关系是 *ToOne-relations (OneToOne, ManyToOne)。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2017-09-15
    • 2018-01-12
    相关资源
    最近更新 更多