本文介绍了@OneToOne以ManyToOneType的形式返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下POJO:

  
公共类SampleBean1 {
@Id
@GeneratedValue(generator =system-uuid)
@GenericGenerator(name =system-uuid,strategy =uuid)
保护字符串ID;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name =OneToOneID)
保护SampleBean1 oneToOne;

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

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name =ManyToOneID)
保护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;

...
}

库来检测OneToOne或ManyToOne(并进行适当的操作)。它总是以ManyToOne的形式出现。



 
//获取类'metadata
ClassMetadata对于(String propertyName:cmd.getPropertyNames()){
org.hibernate.type)来说,cmd = sf.getClassMetadata(o.getClass());

类型propertyType = cmd.getPropertyType(propertyName);

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

}
$ b $ p






$ b $ to $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
oneToOne = false

在调试器中,oneToOne的类型是ManyToOneType!我做错了什么,或者这是一个Hibernate的缺陷?



编辑:这是OneToOne的工作原理。让我们创建三个SampleBeans(SB1,SB2,SB3),如下面的注释中所述。首先,POJO表格中的数据:

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

再次以数据库形式存储数据:

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

只要OneToOneID具有唯一的约束,这种类型的关系是OneToOne?有没有另一种方法来模拟OneToOne?请注意,上面的POJO是单向OneToOne。这可能是问题吗?

解决方案

现在更清晰了,谢谢。



在这两种情况下(例如实体本身和OneToOne映射属性)它是真的 SampleBean1 还是它是一个错字?如果它们是相同的,我很确定它是非法的(你怎么想这个映射会起作用)?我有点惊讶,它被悄悄地降级为多对一而不是抛出一个错误,但也许这就是Hibernate Annotations mapper的功能。


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?

解决方案

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.

这篇关于@OneToOne以ManyToOneType的形式返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 02:51