我有2个实体,通过@OneToOne
注释连接。首先是我的课程,以便更好地理解:
第二实体:
@Table(name = "REASON_FOR_CLIENT_CHEQUE_PAYMENT_REVERSAL")
@Entity
public class ReversalReason implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3338809410372872259L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer code;
private boolean tax;
private boolean currency;
private boolean amountChange;
private boolean locationChange;
private boolean recipient;
private boolean period;
public ReversalReason() {
}
}
基础实体:
@Table(name = "CLIENT_CHEQUE_PAYMENT")
@Entity
public class ClientChequePayment extends BaseDomainObject implements Serializable {
/** Serial version UID */
private static final long serialVersionUID = -1988633355L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer code;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "code", nullable = true)
private ReversalReason reasonForReversal;
@Column(nullable = true, length = 50)
private String proposeReversalUser;
@Column(length = 50, unique = true)
private String displayCode;
@Column(length = 50)
private String originalEntry;
private PaymentStatus originalPaymentStatus;
我确实省略了一些字段,否则代码会太多。现在让我们来解决问题:
在我的计划中,我希望能够通过调用reasonForReversal
设置ClientChequePayment
的字段session.update(clientChequePayment);
。我在调用update
之前检查了字段是否已设置。问题是,hibernate确实为ReversalReason
创建了一个条目,但它没有在ReversalReason
条目中将ClientChequePayment
的PK设置为FK。因此,我可以为1 ReversalReason
创建多个ClientChequePayment
enries。
我的地图是否正确(我无需通过ClientChequePayment
访问ReversalReason
,反之亦然)?如果不需要映射是双向的吗?
答案 0 :(得分:1)
继续发表评论。
问题是,您需要一个具有REASON_FOR_CLIENT_CHEQUE_PAYMENT_REVERSAL
外键的列。
意思是,您的表格CLIENT_CHEQUE_PAYMENT
需要REASON_FOR_CLIENT_CHEQUE_PAYMENT_REVERSAL
的外键,例如id_reversal_reason
,然后您可以这样做:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id_reversal_reason", nullable = true)
private ReversalReason reasonForReversal;
连接列位于源表的拥有表上。
来自name
javadocs @JoinColumn
的摘录:
(Optional) The name of the foreign key column.
* The table in which it is found depends upon the
* context.
* <ul>
* <li>If the join is for a OneToOne or ManyToOne
* mapping using a foreign key mapping strategy,
* the foreign key column is in the table of the
* source entity or embeddable.