@OneToOne getting returned as ManyToOneType

流过昼夜 提交于 2020-01-01 19:22:51

问题


I have the following POJO:


public class SampleBean1 {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    protected String id;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="OneToOneID")
    protected SampleBean1 oneToOne;

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn(name="OneToManyID")
    protected List<SampleBean1> oneToMany;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="ManyToOneID")
    protected SampleBean1 manyToOne; 

    @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinTable(name="SampleBeanManyToMany",
            joinColumns={@JoinColumn(name="LeftID")},
            inverseJoinColumns={@JoinColumn(name="RightID")})
    @IndexColumn(name="ManyToManyIndex")
    protected List<SampleBean1> manyToMany;

    ...
}

I'm making a library to detect OneToOne or ManyToOne (and doing appropriate operations). It always comes back as ManyToOne.

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

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

//Handle ___ToOne
if (propertyType.isEntityType()){
    EntityType et=(EntityType)propertyType;
    System.out.printf("%s=%s\n",propertyName,et.isOneToOne()?"true":"false");
}

}

Here's what I get back:

manyToOne=false
oneToOne=false

In the debugger the Type of the "oneToOne" is ManyToOneType!! Did I do something wrong or is this a Hibernate defect?

EDIT: Here's how the OneToOne's can work. Let's create three SampleBeans (SB1, SB2, SB3) as described in a comment below. First, the data in POJO form:

SB1.oneToOne=SB2
SB2.oneToOne=SB3
SB3.oneToOne=null

Again the data in database form:

ID|OneToOneID
1|2
2|3
3|null

As long as OneToOneID has a unique constraint, would this type of relation be OneToOne? Is there another way to model OneToOne? Note that the POJO above is intended unidirectional OneToOne. Could that be the issue?


回答1:


That's much clearer now, thank you.

Is it really SampleBean1 in both cases (e.g. entity itself and the OneToOne mapped property) or is it a typo? If they are the same, I'm pretty sure it's illegal (how do you imagine that mapping would work)? I'm a bit surprised it's quietly downgraded to "many-to-one" instead of throwing an error, but perhaps that's what Hibernate Annotations mapper does.




回答2:


More a possibility

        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");
            }
        }


来源:https://stackoverflow.com/questions/1381862/onetoone-getting-returned-as-manytoonetype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!