can someone please explain me @MapsId in hibernate?

后端 未结 5 1663
天涯浪人
天涯浪人 2020-11-29 03:34

Can someone please explain to me @MapsId in hibernate? I\'m having a hard time understanding it.

It would be great if one could explain it with an examp

5条回答
  •  死守一世寂寞
    2020-11-29 03:51

    I found this note also useful: @MapsId in hibernate annotation maps a column with another table's column.

    It can be used also to share the same primary key between 2 tables.

    Example:

    @Entity
    @Table(name = "TRANSACTION_CANCEL")
    public class CancelledTransaction {
        @Id
        private Long id; // the value in this pk will be the same as the
                         // transaction line from transaction table to which 
                         // this cancelled transaction is related
    
        @OneToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "ID_TRANSACTION", nullable = false)
        @MapsId
        private Transaction transaction;
        ....
    }
    
    @Entity
    @Table(name = "TRANSACTION")
    @SequenceGenerator(name = "SQ_TRAN_ID", sequenceName = "SQ_TRAN_ID")
    public class Transaction  {
        @Id
        @GeneratedValue(generator = "SQ_TRAN_ID", strategy = GenerationType.SEQUENCE)
        @Column(name = "ID_TRANSACTION", nullable = false)
        private Long id;
        ...
    }
    

提交回复
热议问题