繁体   English   中英

Hibernate OneToOne映射问题

[英]Hibernate OneToOne Mapping Issue

我有三个实体如下:

  1. 属性
  2. Property_Document
  3. Property_User

PropertyProperty_Document具有一对多关系。
PropertyProperty_User具有一对一的关系。

在数据库级别,Property_User具有外键“property_id”,返回Property表。

我正在使用注释来定义实体之间的映射和关系。

Property.java

public class Property {
    //.....
    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="property_id")
    private List<PropertyDocuments> docs;

    @OneToOne(cascade=CascadeType.ALL)
    private PropertyUser owner;
    //....
}

PropertyUser.java

public class PropertyUser {
    @Id
    private int id;

    @column
    private String name;
    //......
}

当它获取属性时,文档列表成功获取,但当它尝试获取用户信息时,它会显示错误消息。

column: owner_id doesn't exist.

请帮忙。 谢谢。

您需要“属性”表中的列来保存它引用的用户的ID。 由于您没有在映射中指定任何列,因此Hibernate使用默认列名称,即owner_id(属性的名称,后跟它引用的列的名称)。 如果你想使用另一个列名,你必须告诉Hibernate:它无法猜测它:

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "the_column_containing_the_id_of_the_referenced_user")
private PropertyUser owner;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM