wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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

karthik sai
Greenhorn

Joined: Jan 06, 2005
Posts: 4
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

Joined: Mar 07, 2010
Posts: 1648
    
    7

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.


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

Joined: Mar 01, 2009
Posts: 1621
    
  13

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
 
 
subject: JPA - shared composite primary key id is not working using @EmbeddedId
 
Similar Threads
Use of set tag in Hibernate mapping file for foreign key
hibernate 1:M relationships & cascade
Testing @ManyToOne Relationships
Not able to get a Session Factory Object
hibernate 1-many insertion