简体   繁体   English

如何正确设置休眠单向一对一映射

[英]how to properly set up hibernate unidirectional one-to-one mapping

i have two tables which has one-to-one mapping on a column. 我有两个表在列上具有one-to-one映射。 i'd like to set up unidirectional one-to-one hibernate mapping using annotations because i want to be able to get results from joined tables, but do not want to do foreign constraint check for child table. 我想使用注释设置unidirectional one-to-one休眠映射,因为我希望能够从联接表中获取结果,但不想对子表进行外部约束检查。

here's my quick code. 这是我的快速代码。

@Entity
@Table(name = "student")
public class student {

// primary key and other column definition here..

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="address_id")
    private Address address;

// getters and //setters
}

@Entity
@Tale(name = "address") 
public class address {
    @Id
    @GeneratedValue(generator = "hibernate-uuid")
    @GenericGenerator(name = "hibernate-uuid", strategy = uuid")
    @Column(name = "id", unique = true, length = 36, nullable = false)
    private String id;

    @Column(name="address_id")
    private String address_id;

    // getters and setters
}

above gives me following exception when i try to insert into student (but not the address). 当我尝试插入学生时(但不是地址),以上给出了以下异常。

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.test.Student from com.test.Address has the wrong number of column. should be 2

what am i doing wrong here? 我在这里做错了什么?

First, change cascade type to REFRESH in student class (better to remove it). 首先,在student课堂中将层叠类型更改为REFRESH(最好将其删除)。 Then if it is unidirectional, means the other part won't know anything of it. 然后,如果它是单向的,则意味着另一部分将一无所知。 So you should add referenceColumnName to your @OneToOne 因此,您应该将referenceColumnName添加到您的@OneToOne

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PK_OF_THE_OTHER_TABLE", referencedColumnName = "CURRENT_TABLE_COLUMN_REFERENCE", nullable = false, insertable = false, updatable = false) @JoinColumn(name = "PK_OF_THE_OTHER_TABLE", referencedColumnName = "CURRENT_TABLE_COLUMN_REFERENCE", nullable = false, insertable = false, updatable = false) @JoinColumn(name = "PK_OF_THE_OTHER_TABLE", referencedColumnName = "CURRENT_TABLE_COLUMN_REFERENCE", nullable = false, insertable = false, updatable = false) @JoinColumn(name = "PK_OF_THE_OTHER_TABLE", referencedColumnName = "CURRENT_TABLE_COLUMN_REFERENCE", nullable = false, insertable = false, updatable = false) @JoinColumn(name = "PK_OF_THE_OTHER_TABLE", referencedColumnName = "CURRENT_TABLE_COLUMN_REFERENCE", nullable = false, insertable = false, updatable = false)

By this implementation, you will not update the other part but joining and get information you need 通过此实现,您将不需要更新其他部分,而是可以加入并获取所需的信息。

UPDATE -- PK_OF_THE_OTHER_TABLE is the primary key of address table. 更新 PK_OF_THE_OTHER_TABLEaddress表的主键。 Also CURRENT_TABLE_COLUMN_REFERENCE is your Foreign Key (current table's field which is pointing to address table) - I assumed you put your one to one relationship in Student entity. CURRENT_TABLE_COLUMN_REFERENCE也是您的外键(当前表的字段指向address表)-我假设您将一对一关系放在Student实体中。

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

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