Hibernate One to One id 为空,为什么?

Hibernate One to One id is null, why?

我有代码:

financialTransactionEntity.getFinancialTransaction_5000().setFinancialTransactionEntity(financialTransactionEntity);

和 类 我有:

@Entity
@Table(name = "master_card_daily_financial_transaction_entity")
public class FinancialTransactionEntity {

    private Long id;

    private FinancialTransaction_5000 financialTransaction_5000;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(mappedBy = "financialTransactionEntity",
            targetEntity = FinancialTransaction_5000.class,
            cascade = javax.persistence.CascadeType.ALL)
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public FinancialTransaction_5000 getFinancialTransaction_5000() {
        return financialTransaction_5000;
    }

    public void setFinancialTransaction_5000(FinancialTransaction_5000 financialTransaction_5000) {
        this.financialTransaction_5000 = financialTransaction_5000;
    }
}

--

@Entity
@Table(name = "master_card_daily_financial_transaction_5000")
public class FinancialTransaction_5000 {

    private Long id;
    private FinancialTransactionEntity financialTransactionEntity;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(targetEntity = FinancialTransactionEntity.class,
            cascade = javax.persistence.CascadeType.ALL)
    @Cascade({org.hibernate.annotations.CascadeType.ALL})
    public FinancialTransactionEntity getFinancialTransactionEntity() {
        return financialTransactionEntity;
    }

    public void setFinancialTransactionEntity(FinancialTransactionEntity financialTransactionEntity) {
        this.financialTransactionEntity = financialTransactionEntity;
    }
}

但是我会在数据库中得到的内容如下:

table: master_card_daily_financial_transaction_entity
+-----+------------------------------+
| id  | financialTransaction_5000_id |
+-----+------------------------------+
|   1 |                         NULL |

table: master_card_daily_financial_transaction_5000
+-----+-------------------------------+
| id  | financialTransactionEntity_id |
+-----+-------------------------------+
|   1 |                          1    |

如何确定我的 financialTransaction_5000_id 也是 1

删除 @OneToOne 中的 mappedBy 参数

来自 @OneToOne.mappedBy() javadoc:

The field that owns the relationship. This element is only specified on the inverse (non-owning) side of the association.

你只需要在 "non-owning" 实体上指定,所以我猜你要么坚持错误的关联,要么 mappedBy 错误的一面。