How to use two foreign keys as primary key on Hibernate entity annotation

Zack

I wanna make a primary key from 2 foreign keys using Hibernate entity annotation : look at the picture please **

  • How can i make this two foreign keys "comID" and "reference" as Primary Key for LigneCommande table with Hibernate annotation !! Thank you :)

I tried this code but it didnt work :

Class "Produit" :

public class Produit implements Serializable{

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "LigneCommande", catalog = "mkyongdb", joinColumns = { 
            @JoinColumn(name = "reference", nullable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "commande_id", 
                    nullable = false, updatable = false) })
    private List<Commande> commande;


    public List<Commande> getCommande() {
        return commande;
    }

    public void setCommande(List<Commande> commande) {
        this.commande = commande;
    }
}

Class "Commande" :

@Entity
public class Commande implements Serializable{

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "commande")
    private List<Produit> produit;

public List<Produit> getProduit() {
        return produit;
    }

    public void setProduit(List<Produit> produit) {
        this.produit = produit;
    }
}

On top of that I have not any exception or error !!

Zack

This is the solution :

public class LigneCommande implements Serializable {

@EmbeddedId
    protected LigneCommandePK ligneCommandePK;

    @Column(name = "quantite")
    private int quantite;

    @Column(name = "status")
    private String status;
    @JoinColumn(name = "produit_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Produit produit;
    @JoinColumn(name = "commande_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Commande commande;
}

This the Class "Produit" :

@Entity
@Table(name = "produit")
public class Produit implements Serializable {@OneToMany(cascade = CascadeType.ALL, mappedBy = "produit")
    private Collection<LigneCommande> ligneCommandeCollection;
}

And this is the association class :

@Embeddable
public class LigneCommandePK implements Serializable {
    @Basic(optional = false)
    @Column(name = "commande_id")
    private int commandeId;
    @Basic(optional = false)
    @Column(name = "produit_id")
    private int produitId;
}

And it works, look at the picture : enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Two foreign keys as primary key

From Dev

Use foreign keys as primary key

From Dev

How to use a Primary Key also as a Foreign Key reference with JPA and Hibernate?

From Dev

how to make two column as a primary key in hibernate annotation class

From Java

Composite primary key of two foreign keys

From Dev

Using two foreign keys as a primary key - MySQL

From Dev

When we create a primary key using hibernate we use @Id as annotation but what annotation should be used to define a foreign key

From Dev

how to connect two table by the use of primary-foreign key?

From Dev

How to assign primary key values into foreign keys?

From Dev

How does hibernate annotation generate the primary key?

From Dev

How can I make a primary key consisting of two foreign keys, 'be the same key', whatever order the values are?

From Dev

Hibernate Mapping composite primary key that contains foreign keys

From Dev

Entity Framework: Composite Foreign Key on unique (not primary keys) parent fields

From Dev

Entity Framework - Composite Primary Key formed by 3 Foreign Keys

From Dev

Hibernate with two foreign keys from same table- annotation

From Dev

Hibernate - Foreign and primary keys in inheritance

From Dev

Can we have two primary keys in one Foreign key field?

From Dev

two foreign keys to same primary key select statement MYSQL

From Dev

How to denote primary key and foreign key in my entity relationship diagram?

From Dev

Hibernate Primary Foreign Key field

From Dev

Hibernate Primary Foreign Key field

From Dev

Hibernate String Primary Key with Annotation

From Dev

Hibernate String Primary Key with Annotation

From Dev

Hibernate foreign key with a part of composite primary key

From Dev

How to use SUM when foreign key and primary key matched in MYSQL

From Dev

Mapped foreign key in Hibernate Entity

From Dev

One primary key in one table linked to two foreign keys in two different tables?

From Dev

One primary key in one table linked to two foreign keys in two different tables?

From Dev

Hibernate interprets two foreign key relations as composite primary key(MappingException). Why?

Related Related

  1. 1

    Two foreign keys as primary key

  2. 2

    Use foreign keys as primary key

  3. 3

    How to use a Primary Key also as a Foreign Key reference with JPA and Hibernate?

  4. 4

    how to make two column as a primary key in hibernate annotation class

  5. 5

    Composite primary key of two foreign keys

  6. 6

    Using two foreign keys as a primary key - MySQL

  7. 7

    When we create a primary key using hibernate we use @Id as annotation but what annotation should be used to define a foreign key

  8. 8

    how to connect two table by the use of primary-foreign key?

  9. 9

    How to assign primary key values into foreign keys?

  10. 10

    How does hibernate annotation generate the primary key?

  11. 11

    How can I make a primary key consisting of two foreign keys, 'be the same key', whatever order the values are?

  12. 12

    Hibernate Mapping composite primary key that contains foreign keys

  13. 13

    Entity Framework: Composite Foreign Key on unique (not primary keys) parent fields

  14. 14

    Entity Framework - Composite Primary Key formed by 3 Foreign Keys

  15. 15

    Hibernate with two foreign keys from same table- annotation

  16. 16

    Hibernate - Foreign and primary keys in inheritance

  17. 17

    Can we have two primary keys in one Foreign key field?

  18. 18

    two foreign keys to same primary key select statement MYSQL

  19. 19

    How to denote primary key and foreign key in my entity relationship diagram?

  20. 20

    Hibernate Primary Foreign Key field

  21. 21

    Hibernate Primary Foreign Key field

  22. 22

    Hibernate String Primary Key with Annotation

  23. 23

    Hibernate String Primary Key with Annotation

  24. 24

    Hibernate foreign key with a part of composite primary key

  25. 25

    How to use SUM when foreign key and primary key matched in MYSQL

  26. 26

    Mapped foreign key in Hibernate Entity

  27. 27

    One primary key in one table linked to two foreign keys in two different tables?

  28. 28

    One primary key in one table linked to two foreign keys in two different tables?

  29. 29

    Hibernate interprets two foreign key relations as composite primary key(MappingException). Why?

HotTag

Archive