Hibernate反向一对一的非定向映射

时间:2017-05-09 06:43:46

标签: java hibernate

我有两个班级CardsCardBalance。在我的数据库中,card_balancecards上有外键。但是在ORM中,我希望实体Cards具有属性CardBalance,而实体CardBalance没有属性Cards。 我尝试以下一种方式做到这一点:

@Entity
@Table(name = "CARD_BALANCE")
public class CardBalance {

    @Id
    @Column(name = "BALANCE_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CARD_BALANCE_SEQ")
    @SequenceGenerator(name = "CARD_BALANCE_SEQ", sequenceName = "CARD_BALANCE_SEQ")
    private Long balanceId;

    @Column(name="CARD_ID")
    private Long cardId;

}


@Entity
@Table(name = "CARDS")
public class Cards implements Serializable {

    @Id
    @Column(name = "CARD_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CARDS_SEQ")
    @SequenceGenerator(name = "CARDS_SEQ", sequenceName = "CARDS_SEQ")
    private Long cardId;

    @Column(name = "CARD_NAME", length = 30, unique = true)
    private String cardName;

    @Column(name="PERSON_ID")
    private Long personId;

    @Column(name = "CARD_KEY", nullable = false)
    private long cardKey;

    @OneToOne
    @JoinColumn(name="TYPE_ID", nullable = false)
    private TypeCard typeCard;

    @OneToOne
    @JoinColumn(name="CARD_ID", nullable = false)
    private CardBalance cardBalance;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name="CARD_ID")
    public Set<BalanceHist> balanceHists = new HashSet<>();

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name="CARD_ID")
    public Set<Events> events = new HashSet<>();

}

但它不起作用。我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

首先,你的@JoinColumn中有错,应该是:

@OneToOne
@JoinColumn(name="BALANCE_ID", nullable = false)
private CardBalance cardBalance;