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=falseIn 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=nullAgain the data in database form:
ID|OneToOneID 1|2 2|3 3|nullAs 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?
That’s much clearer now, thank you.
Is it really
SampleBean1in 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.