Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JPA no primary key

843830Mar 10 2010 — edited Aug 18 2010
hi ,
I have a some tables with no primary key, I'm mapping jpa entity beans, but it gives me error because I need to have a primary key. I was looking at Generated primary keys but i believe they require me to have a separate table in the database or and Id field in the table, but i cannot create any table in database or any new column because some dba issues. So i need to work around and simply insert data to those tables with no primary key. can someone tell me what is the best solution for this type of scenario.
package ejb.entity.cba;

import java.io.Serializable;
import java.sql.Date

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
@Table(name = "OCD")
public class CBAOutlineCertifiedDetail implements Serializable {



//columns with no primary key 
private String accountNumber;
private String accountingLineItem;
private int    cbaId; 

//getters and setter 

}

..............
Edited by: s_dhillon on Mar 10, 2010 10:53 PM

Comments

801449
Well an entity needs an id, there is no way around it. However you could map more than one table to one entity. ( SecondaryTable ) The name of the class suggests that there is a CBAOutlineCertified object as well. Couldn't you map this table to it? Is there a foreign key on this table?

If that can't be done, I'm afraid you'll have to forget entities and use native queries to manage operations on this table.

Btw. couldn't the cbaId be used as an Id field? The name sort of suggests that it is the id of this entity.

-Zsom-
843830
thanks Zsom, there is no foreign key on this tale and it's just and independednt table that i need to insert data into. It's more of warehousing table. I read somewhere that i can make all the colums with @Id and that will create a uniqueness to surpass my issue of not modifying tables in the database.
r035198x
Id's don't have to be generated. Just use the fields in the tables that are already primary keys.
If there are tables without primary keys (shudder), then use the unique columns in them. You can create primary key classes for those properties.
That way you stick with the more portable JPA constructs too.
843830
Is this issue solved? I am facing the same issue. The db table doesn't have any primary / unique key. Its a log table. Each column can also have null value. How can I solve this?????
PhHein
Welcome to the forum. Please don't post in threads that are long dead and don't hijack other threads. When you have a question, start your own topic. Feel free to provide a link to an old post that may be relevant to your problem.

I'm locking this thread now.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 15 2010
Added on Mar 10 2010
5 comments
6,856 views