0

Good evening. When I attempt to create jpa entities using a database connection I am getting some exceptions. Something about id non-existent. I suspect it has something to do with the fact that some of the tables have no primary key. Because when I go through each entity and give it a primary key, the error clears up. How can I create entities on tables that have no primary keys? I am using websphere integration developer (wid) btw.

SoftwareSavant
  • 9,467
  • 27
  • 121
  • 195

1 Answers1

1

All entities have an Id, that is how they work - or more accurately how the specification states it. If there are tables in the database you want to map to entities and the tables don't have a primary key there are several options availble to you.

  • Add a generated Id (managed by JPA or DB) to the table - suggested if you have that freedom.
  • Use a couple of the existing columns in the table as the entity Id (choosing the least numbers necessary to make sure the Id becomes unique).
  • Use all columns, even if there is a theoretical chance of their combination not being unique (would mess up the table/db so care should be taken even in the java layers to not let that happen)
  • ... might be more alternatives I can't think of right now

Should be rather easy to find information about how to do alternative 2 and 3 via a search for JPA and CompositeKey.

Good Luck

esej
  • 3,059
  • 1
  • 18
  • 22