Hibernate Annotations(二)

來源:互聯網
上載者:User

複合主鍵的應用 (EmbeddedId)

主表


@Entity
public class A implements Serializable...{

    @EmbeddedId
    private AId aId;

    public AId getAId() ...{
        return aId;
    }

    public void setAId(AId aId) ...{
        this.aId = aId;
    }
}
複合主鍵

 

@Embeddable
public class AId implements Serializable ...{

    @OneToOne
    @JoinColumn( name = "bid" )
    private B b;

    @OneToOne
    @JoinColumn( name = "cid" )
    private C c;


    public B getB() ...{
        return b;
    }

    public void setB(B b) ...{
        this.b = b;
    }

    public C getC() ...{
        return c;
    }

    public void setC(C c) ...{
        this.c = c;
    }
}
關聯表

 

@Entity
public class B ...{

    @Id
    @GeneratedValue
    private Long id;


    public Long getId() ...{
        return id;
    }

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

@Entity
public class C ...{

    @Id
    @GeneratedValue
    private Long id;


    public Long getId() ...{
        return id;
    }

    public void setId(Long id) ...{
        this.id = id;
    }
}
複合主鍵的應用 (IdClass)

主表

 

@Entity
@IdClass(OrderLinePk.class)
public class OrderLine ...{
    @Id
    public Order order;
    @Id
    public Product product;
}
主鍵類

 

public class OrderLinePk implements Serializable ...{
    @ManyToOne
    @JoinColumn(name = "foo", nullable = false)
    public Order order;
    @ManyToOne
    @JoinColumn(name = "bar", nullable = false)
    public Product product;   
}

關聯表

 

@Entity
public class Product ...{
    @Id
    public String name;
}

@Entity
@Table(name = "OrderTableFoobar")
public class Order ...{
    @Id
    @GeneratedValue
    public Integer id;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.