2017-05-09 171 views
0

我有兩個類CardsCardBalance。在我的DBsсhema表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<>(); 

} 

但簡化版,它的工作。我如何解決這個問題?

回答

0

首先,你必須在你的@JoinColumn一個錯誤,應該是:

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