Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 使用类元数据确定manytoman与OneToMany_Hibernate_Many To Many - Fatal编程技术网

Hibernate 使用类元数据确定manytoman与OneToMany

Hibernate 使用类元数据确定manytoman与OneToMany,hibernate,many-to-many,Hibernate,Many To Many,我使用ClassMetadata来确定hibernate POJO的结构 我需要确定一个集合是一个集合还是多个集合。这些信息在哪里?我能不使用反射就得到它吗?请参阅下面的代码 //Get the class' metadata ClassMetadata cmd=sf.getClassMetadata(o.getClass()); for(String propertyName:cmd.getPropertyNames()){ if (cmd.getPropertyType(prop

我使用ClassMetadata来确定hibernate POJO的结构

我需要确定一个集合是一个集合还是多个集合。这些信息在哪里?我能不使用反射就得到它吗?请参阅下面的代码


//Get the class' metadata
ClassMetadata cmd=sf.getClassMetadata(o.getClass());

for(String propertyName:cmd.getPropertyNames()){
    if (cmd.getPropertyType(propertyName).isCollectionType() && cmd.??()) //Do something with @ManyToMany collections.
}

我所需要的只是告诉我这是否是一种多对多的关系。我看到了getPropertyLaziness(),但这并不总是保证集合的类型。有什么想法吗?

不幸的是,没那么简单。检测此问题的最佳方法是检查特定的
CollectionPersister
实现:

SessionFactory sf = ...;

// Get the class' metadata
ClassMetadata cmd = sf.getClassMetadata(o.getClass());

for(String propertyName:cmd.getPropertyNames()) {
  Type propertyType = cmd.getPropertyType(propertyName);
  if (propertyType.isCollectionType()) {
    CollectionType collType = (CollectionType) propertyType;

    // obtain collection persister
    CollectionPersister persister = ((SessionFactoryImplementor) sf)
      .getCollectionPersister(collType.getRole());

    if (persister instanceof OneToManyPersister) {
      // this is one-to-many
    } else {
     // this is many-to-many OR collection of elements
    }
  } // if
} // for

这是一种可能性

直接已知子类:ManyToOneType,OneToOneType

       SessionFactory sf = ...;

       ClassMetadata cmd = sf.getClassMetadata(o.getClass());

       for (String propertyName : cmd.getPropertyNames()) {
            Type propertyType = cmd.getPropertyType(propertyName);

            if (propertyType.isEntityType()) {
                EntityType entityType = (EntityType) propertyType;

                if (entityType instanceof ManyToOneType) {
                    System.out.println("this is ManyToOne");
                } else if (entityType instanceof OneToOneType) {
                    System.out.println("this is OneToOne");
                }
            }
        }

哦,等等。我想我找到了。EntityType.isOneToOne()。然而,没有你,我不可能找到这个。我真的很感谢你的帮助!你的作品对我和其他人都有帮助。干得好!我想我说得太快了。由于某些原因,EntityType.isOneToOne()始终为false。我没有找到一个一对一的复印机。有什么想法吗?
EntityType.isOneToOne()
是一个不错的选择。或者,您可以检查属性类型是否为OneToOneType的
instanceof
,这是相同的。不过,您永远不会将一对一作为集合的一部分-您正在检查一些其他属性,对吗?它们首先使用isEntityType()进行检查。然后转换为EntityType。总是假的。它几乎是从一个(可能)显示源代码的站点上查看的:。有什么想法吗?另外,调试器将@OneToOne实体显示为manytone。他们必须为@OneToOne和@ManyToOne重用同一个类。你也看到了吗?