@OneToOne returns as ManyToOneType

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 create a library to detect OneToOne or ManyToOne (and perform the appropriate operations). He 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");
}

} Code>

Here is what I will return:

manyToOne = false
oneToOne = false

In the debugger, the type "oneToOne" is ManyToOneType! Am I doing something wrong or is it sleep mode?

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

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

Again the data is in the form of a database:

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

OneToOneID , OneToOne? OneToOne? , POJO OneToOne. ?

+1
2

, .

SampleBean1 (, OneToOne) ? , , ( , )? , "--" , , , , , Hibernate Annotations.

+1

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

Source: https://habr.com/ru/post/1716822/


All Articles