File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes JPA  - shared composite primary key id is not working using @EmbeddedId Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA  - shared composite primary key id is not working using @EmbeddedId" Watch "JPA  - shared composite primary key id is not working using @EmbeddedId" New topic

JPA - shared composite primary key id is not working using @EmbeddedId

karthik sai

Joined: Jan 06, 2005
Posts: 4
JPA Toplink - shared composite primary key id is not working using @EmbeddedId

This is the table script
my_salary BIGINT NOT NULL,
my_name VARCHAR(255), PRIMARY KEY (DEPARTMENT, STATE, my_salary))

The composite primary key is (DEPARTMENT, STATE, my_salary)

I have a which implements Serializable
protected long salary;

public long getSalary() {
return salary;

EmployeeIdPK extends AbstraceIdentiferPK
protected String department;

protected String state;

public EmployeeId(String department, String state, long salary) {
this.department = department;
this.state = state;
this.salary = salary;

In the

private EmployeeId id;

private String name;

public Employee(EmployeeId id) { = id;

public EmployeeId getId() {
return id;

In the

es.createEmployee(new EmployeeId("marketing", "MA", 1000000), "shekar");
es.createEmployee(new EmployeeId("marketing", "NY",800000), "Bill Clinton");
es.createEmployee(new EmployeeId("engineering", "MA",700000),"Angela Caicedo");

This is the peristed output

******** Table: EMPLOYEE *******
| Angela Caicedo | engineering | MA |
| shekar | marketing | MA |
| Bill Clinton | marketing | NY |

The issue , the salary is not persisted in the database

Please let me know if i m missing anything.

Bill Gorder

Joined: Mar 07, 2010
Posts: 1682


Please edit your post and UseCodeTags for your code snippets. This makes it much easier to read and increases the likelihood of getting a response.

[How To Ask Questions][Read before you PM me]
chris webster

Joined: Mar 01, 2009
Posts: 2289

Can't help you with JPA, unfortunately, but are you sure about your choice of primary key?

You have put all the attributes of your Employee table - except the Employee's name (my_name) - into the PK. This means:

1. You cannot have two people with the same Department, State and Salary, but you can have two people with the same name.
2. You are indexing virtually the entire contents of your table.
3. The one column that looks it should be a key (my_name) is not indexed or part of a key.

If you're just messing around here with an arbitrary composite key to try out the JPA techniques, then of course this is not a problem. But if you think this is how to define your data and keys for real, then it is fairly catastrophic, and you would really need to read some basic database tutorials.

Also, are you sure you are reading the whole table e.g. "SELECT * FROM Employee" when you check the data? Your table script defines my_salary as NOT NULL, so it should not be possible to write a record with a NULL value in my_salary.

No more Blub for me, thank you, Vicar.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
JPA does not support inheritance with Embeddables.
Either remove the inheritance, or don't use an EmbeddedId, use an IdClass instead and include the id fields in the employee.

Also, your Id does not seem like a very good Id, consider using a generated Id instead.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
I agree. Here's the link:
subject: JPA - shared composite primary key id is not working using @EmbeddedId
jQuery in Action, 3rd edition