This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
karthik sai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPA Toplink - shared composite primary key id is not working using @EmbeddedId

This is the table script
CREATE TABLE Employee (
DEPARTMENT VARCHAR(255) NOT NULL,
STATE VARCHAR(255) NOT NULL,
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 AbstraceIdentiferPK.java which implements Serializable
@Column(name="my_salary")
protected long salary;

public long getSalary() {
return salary;
}

EmployeeIdPK extends AbstraceIdentiferPK
@Column(name="department")
protected String department;

@Column(name="state")
protected String state;

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

In the Employee.java

@EmbeddedId
private EmployeeId id;

@Column(name="my_name")
private String name;

public Employee(EmployeeId id) {
this.id = id;
}

public EmployeeId getId() {
return id;
}

In the JPAMain.java

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 *******
+--------------------------------+--------------------------------+--------------------------------+
| MY_NAME | DEPARTMENT | STATE |
+--------------------------------+--------------------------------+--------------------------------+
| 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
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthik,

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.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic