Two foreign keys as primary key

multiple foreign keys sql
primary key and foreign key in access
composite foreign key
combination of primary key with foreign key forms
what is primary key and foreign key in sql
primary key and foreign key in sql with examples pdf
how to use primary key and foreign key in mysql
foreign key sql

How would I implement the following tables using hibernate annotations?

Current code is: (stripped for brevity)

User

@Entity
@Table(name = "user")
public class User implements java.io.Serializable {
    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
}

SocialNetwork

@Entity
@Table(name = "social_network")
public class SocialNetwork implements java.io.Serializable {
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
}

SocialProfile

@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {
    @Id
    @ManyToOne
    @JoinColumn(name="user_id")
    public User getUser() {
        return user;
    }

    @Id
    @ManyToOne
    @JoinColumn(name="social_network_id")
    public SocialNetwork getSocialNetwork() {
        return socialNetwork;
    }
}

Obviously my code is not working correctly right now. Can anyone shed some light onto this?


you need an embeddable SocialProfileId like this :

@Embeddable
public class SocialProfileId implements Serializable {
    @Column(name = "user_id")
    private long userId;
    @Column(name = "social_network_id")
    private long socialNetworkId;
}

then, your SocialProfile entity will look like this :

@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {

    @EmbeddedId
    private SocialProfileId id;

    @ManyToOne
    @JoinColumn(name="user_id")
    public User getUser() {
        return user;
    }

    @ManyToOne
    @JoinColumn(name="social_network_id")
    public SocialNetwork getSocialNetwork() {
        return socialNetwork;
    }
}

EDIT sorry, i have mixed annotations on fields and methods on my answer ... never do that ! ;-)

Two foreign keys referencing the same primary key, Example SQL code (create with MySQL Workbench): CREATE TABLE `test`.`user​` ( `iduser` INT NOT NULL, `username` VARCHAR(45) NULL,  I have a question regarding a table creation. I want to combine the attributes of "Ono" and "Pno" into a primary key for a new table. These are both foreign keys, each from different tables.


It seems you need mamy-to-many mapping having join-table with extra column. For this you need to create 2 more classes.

FYI : http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

Foreign key, A foreign key joins a table to another table by referencing its primary key. A foreign key constraint specifies that the key can only contain values that are in the referenced primary key, and thus ensures the referential integrity of data that is joined on the two keys. referencing the same primary key another table. i created the table relations using the relations editor in access. even though i could have multiple foreign keys to a table access did allow the referential


My problem for a REST application was similar but a little bit different, I will post it here. I had 2 data tables: Song & Tag with an id each (songid, tagid). Then I had a table for joining them together Tagassignment which only had both of the primary keys from Song and Tag. So I did not want to join them, I wanted to keep the table with both foreign keys.

Source of my solution: http://www.objectdb.com/java/jpa/entity/id


Before

Song

@Entity
@Table(name = "songs")
data class Song(

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id: Int,

    ...
)

Tag

@Entity
@Table(name = "tags")
data class Tag(

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id: Int,

    ...
)

Tagassignment

 @Entity
 @Table(name = "tagassignment")
 data class Tagassignment(

     val songid: Int,

     val tagid: Int

 )

After

I did not change Song and Tag.

Tagassignment

@Entity
@IdClass(value = TagassignmentKey::class)
@Table(name = "tagassignment")
data class Tagassignment(

    @Id
    val songid: Int,

    @Id
    val tagid: Int

)

and I created a Key class

TagassignmentKey

class TagassignmentKey(val songid: Int, val tagid: Int) : Serializable {

    constructor() : this(0, 0)

}

Foreign and Primary Key Differences (Visually Explained , For an example, see Add multiple foreign key constraints to a single column. the FOREIGN KEY constraint and the referenced table's primary key must contain​  Primary key and foreign key are the two most important and common types of keys used in relational databases. A primary key is a special key used to uniquely identify records in a table, whereas a foreign key is used to establish relationship between two tables.


In addition to what Pras has said, we can even move the @ManyToOne inside the @Embeddable class itself so SocialProfileId will look like, where you can clearly see in your Embedded class itself that it referencing another table

@Embeddable
public class SocialProfileId implements Serializable {

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User userId;


    @ManyToOne
    @JoinColumn(name = "social_network_id")
    private SocialNetwork socialNetworkId;

    public SocialProfileId(User userId, SocialNetwork socialNetworkId) {
        this.userId = userId;
        this.socialNetworkId = socialNetworkId;
    }

}

And your SocialProfile entity can simply have the embedded class and other fields like email and so on..

@Entity
@Table(name = "social_profile")
public class SocialProfile implements java.io.Serializable {

    @EmbeddedId
    private SocialProfileId id;

    @Column(name="email")
    private String email;
}

Is it possible to reference one column as multiple foreign keys , Two Foreign Keys refering to a primary key. MySQL Database Forums on Bytes. In a foreign key reference, a link is created between two tables when the column or columns that hold the primary key value for one table are referenced by the column or columns in another table. This column becomes a foreign key in the second table.


Two foreign keys referring to one primary key, I have two tables: Table1 idproduct (PK) des_product Table2 idclient a traditional primary key and have the two id columns as foreign keys. A foreign key joins a table to another table by referencing its primary key. A foreign key constraint specifies that the key can only contain values that are in the referenced primary key, and thus ensures the referential integrity of data that is joined on the two keys. You can identify a table's foreign key when you create the table, or in an existing table with ALTER TABLE.


Foreign Key Constraints, In the context of relational databases, a foreign key is a set of attributes subject to a certain kind Because the primary key of a table must be unique, and because A table may have multiple foreign keys, and each foreign key can have a  A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.


Foreign Key Constraint, Yes (if I'm correctly understanding the question). Consider an associative entity: A ID (pk) … (other elements) B ID (pk) … (other elements) AB  Composite keys are a very powerful relational database concept and we took good care to make sure Doctrine 2 supports as many of the composite primary key use-cases. For Doctrine 2.0 composite keys of primitive data-types are supported, for Doctrine 2.1 even foreign keys as primary keys are supported.