Questions tagged [composite-primary-key]

Composite primary key is a primary key, which consists of more than one column. The column combination guarantees the uniqueness of the PK.

A Composite Primary Key is a Composite Key, which is assigned as Primary.

Primary key is a single field or the least combination of fields that uniquely identifies each record in table. When the primary key consists of more than one field, when we are talking about composite primary key. Composite or not, is the most appropriate key to be main key of reference for the table.

The primary key must not be null and must contain only unique values.

Primary keys are mandatory for every table. When choosing a primary key from the pool of candidate keys, always choose a single simple key over a composite key.

Links

984 questions
11
votes
1 answer

JPA Query returning nulls - Composite Key with null column

I have a legacy database (Cobol files actually) that I am accessing using a proprietary JDBC driver with Hibernate/JPA. The Entity has a composite primary key with 2 columns: CODE and SITE. In the legacy data there are records for the same CODE that…
DuncanKinnear
  • 4,563
  • 2
  • 34
  • 65
11
votes
2 answers

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

This is my annotation class and i want userId and groupId column both as primary key. I have found more questions (Question) about this, but didn't found relevant answer. I have less reputation, so I am not able to comment on posts, So I am putting…
10
votes
4 answers

Hibernate foreign key as part of primary key

I have to work with hibernate and not very sure how solve this problem, I've 2 table with a 1..n relationship like this: ------- TABLE_A ------- first_id (pk) second_id (pk) [other fields] ------- TABLE_B ------- first_id (pk)(fk…
rascio
  • 8,968
  • 19
  • 68
  • 108
10
votes
1 answer

Can a Foreign Key be part of a Composite Primary Key for another table?

I have two(of many) tables in a music database: Concerts: ArtistID,ConcertID,ConcetName,VenueID ConcertDetails: ConcertDate, ConcertID, Cost The ConcertDetails tables as you see, uses ConcertID which is also in the Concerts table. I combine…
DJPharaohCHS
  • 181
  • 1
  • 3
  • 13
10
votes
2 answers

Composite Primary Keys example in MySQL

I have encountered MySQL itself recently and the topic of Composite Primary Keys in MySQL, especially how it is useful and what are its pros and cons from this site I wanted to play with that, so I have created three tables in this fashion: CREATE…
user1686230
  • 247
  • 2
  • 6
  • 12
10
votes
4 answers

How to set (combine) two primary keys in a table

For a small sales related application, we designed database using logical data model. Come to the stage to convert in to physical model. While creating table in SQL Server Management Studio Express, according to our logical data model, we need to…
panindra
  • 646
  • 2
  • 11
  • 33
9
votes
2 answers

How to set the column order of a composite primary key using JPA/Hibernate

I'm having trouble with the ordering of the columns in my composite primary key. I have a table that contains the following: @Embeddable public class MessageInfo implements Serializable { private byte loc; private long epochtime; …
Garrett
  • 105
  • 1
  • 4
9
votes
1 answer

Hibernate second-level cache composite-id

I am trying to cache an object in Hibernate's second level cache that has a composite-id mapped in my persistence-mapping file. The logs say that the first time I run the query, the class mapped as the composite-id is put in the cache. However,…
sma
  • 9,449
  • 8
  • 51
  • 80
9
votes
2 answers

Why dropping a primary key is not dropping its unique index?

I have a table with Col1 and Col2 as a composite primary key pk_composit_key and a unique index that was automatically created for the constraint. I then altered the table to add new column Col3. I dropped the pk_composit_key constraint: ALTER…
touchchandra
  • 1,506
  • 5
  • 21
  • 37
9
votes
3 answers

IN clause with a composite primary key in JPA criteria

I have a table named group_table in MySQL with only two columns user_group_id and group_id (both of them are of type VARCHAR). Both of these columns together form a composite primary key. I need to execute a statement using a sub-select IN() to…
Tiny
  • 27,221
  • 105
  • 339
  • 599
9
votes
1 answer

"No mapped field" when using partial query and composite keys in Doctrine2

I have two models called Person and Tag. One Person has many Tags, and the Tag primary key is a composite key of person_id and tag (Person $person and $tag in Doctrine2). There is a data field (BLOB) in the Tag model with a lot of data. I am setting…
Nils
  • 780
  • 5
  • 16
8
votes
5 answers

Hibernate n:m extractHashCode throws NullPointerException

I get the following exception while inserting an object with hibernate. Reading from the database works like a charm. I use MySQL 5.5 as database provider and hibernate 3.6.5. I have the following database…
Florian
  • 93
  • 1
  • 7
8
votes
4 answers

Select the just-inserted record with a composite Primary Key

I have a table with a composite Primary Key, arranged something like this: CREATE TABLE [dbo].[mytable] ( [some_id] [smallint] NOT NULL, [order_seq] [smallint] NOT NULL, -- etc... ) Both of these columns are part of the primary key…
8
votes
1 answer

One-to-One bi-directional mapping with composite primary key

Could someone please help me with doing the bi-directional one-to-one JPA mapping for the following relationship using the composite primary key using Hibernate/JPA?
skip
  • 12,193
  • 32
  • 113
  • 153
8
votes
1 answer

anything wrong about having MANY sequences in postgres?

I am developing an application using a virtual private database pattern in postgres. So every user gets his id and all rows of this user will hold this id to be separated from others. this id should also be part of the primary key. In addition every…