Hibernation / persistence without @Id

I have a database view that gives a result set that does not have a true primary key. I want to use Hibernate / Persistence to map this result set to Java objects. Of course, since there is no PC, I can not decorate any field with @Id .

When deploying, Hibernate complains about the lack of @Id . How can I get around this?

+28
java hibernate hibernate-annotations primary-key
May 29 '09 at 12:54
source share
6 answers

If there is a column combination that makes a unique row, model the primary key class around the column combination. If this does not happen, you are mostly unlucky - but you should redesign the presentation because it probably doesn't make sense.

There are several different approaches:

 @Entity public class RegionalArticle implements Serializable { @Id public RegionalArticlePk getPk() { ... } } @Embeddable public class RegionalArticlePk implements Serializable { ... } 

Or:

 @Entity public class RegionalArticle implements Serializable { @EmbeddedId public RegionalArticlePk getPk() { ... } } public class RegionalArticlePk implements Serializable { ... } 

Details here: http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e1517

Here is a publication that describes a similar problem: http://www.theserverside.com/discussions/thread.tss?thread_id=22638

+23
May 29 '09 at 1:04 pm
source share

For each object, you must specify at least one of the following:

  • one @Id
  • multiple @Id and @IdClass (for composite primary key)
  • @EmbeddedId

maybe you can create a composite primary key containing multiple fields?

+6
May 29, '09 at 13:03
source share

Here is an example that has 2 keys as "Id": https://gist.github.com/3796379

+1
Sep 27
source share

Instead of finding workarounds in Hibernate, it might be easier to add a dummy identifier in the database view. Suppose we have a PostgreSQL view with two columns, and none of them is unique (and there is no primary key, since Postgres does not allow PK or any other restrictions on the views) ex.

 | employee_id | project_name | |:------------|:-------------| | 1 | Stack01 | | 1 | Jira01 | | 1 | Github01 | | 2 | Stack01 | | 2 | Jira01 | | 3 | Jira01 | ------------------------------ 

What is represented by the following query:

 CREATE OR REPLACE VIEW someschema.vw_emp_proj_his AS SELECT DISTINCT e.employee_id, pinf.project_name FROM someschema.project_info pinf JOIN someschema.project_employee pe ON pe.proj_id = pinf.proj_id JOIN someschema.employees e ON e.employee_id = pe.emloyee_id 

We can add a dummy id using row_number ():

 SELECT row_number() OVER (ORDER BY subquery.employee_id) AS row_id 

as in this example:

 CREATE OR REPLACE VIEW someschema.vw_emp_proj_his AS SELECT row_number() OVER (ORDER BY subquery.employee_id) AS row_id, subquery.employee_id, subquery.project_name FROM (SELECT DISTINCT e.employee_id, pinf.project_name FROM someschema.project_info pinf JOIN someschema.project_employee pe ON pe.proj_id = pinf.proj_id JOIN someschema.employees e ON e.employee_id = pe.emloyee_id ) subquery; 

And the table will look like this:

 | row_id | employee_id | project_name | |:------------|:------------|:-------------| | 1 | 1 | Stack01 | | 2 | 1 | Jira01 | | 3 | 1 | Github01 | | 4 | 2 | Stack01 | | 5 | 2 | Jira01 | | 6 | 3 | Jira01 | ------------------------------------------- 

Now we can use row_id as @Id in JPA / Hibernate / Spring Data:

 @Id @Column(name = "row_id") private Integer id; 

As in the example:

 @Entity @Table(schema = "someschema", name = "vw_emp_proj_his") public class EmployeeProjectHistory { @Id @Column(name = "row_id") private Integer id; @Column(name = "employee_id") private Integer employeeId; @Column(name = "project_name") private String projectName; //Getters, setters etc. } 
+1
May 18 '17 at 14:13
source share

You can check if there is a logical logical identifier and display matching information accordingly. Hibernate will not check the database for a specific primary key.

0
May 29 '09 at 13:01
source share

While not quite what you are asking for, here is a little trick that I use. Ask to select "rownum" and define "rownum" as the ID column in the model. This will effectively make each line unique to Hibernate.

-one
Apr 6 '16 at 22:39
source share



All Articles