hibernate - JPA modeling a table without an entity ID -


i'm building simple indexing system list of products. have table of keywords , product ids, , score indicating relevance of keyword product:

create table item_index (   keyword varchar(50) not null,   item_id int not null,   score int not null,   index keyword_index (keyword) ); 

(item_id foreign key item table, not shown here.)

in sql query index follows:

select item_id,score item_index keyword=? order score desc; 

this gives me list of item ids relevant keyword, in order of relevance.

my question is: how can using jpa?

my attempt follows:

@entity @table( name="item_index" ) @namedqueries({   @namedquery( name="itemindex.findbyword", query="select itemindex i.keyword=:keyword order i.score desc" ),   @namedquery( name="itemindex.deleteall", query="delete itemindex" ) }) public class itemindex implements serializable {   private static final long serialversionuid = 1l;    @column( name = "keyword", length=50 )   private string keyword;    @manytoone( fetch = fetchtype.lazy )   @joincolumn( name = "item_id" )   private item item;    @column( name = "score" )   private int score;    /* getters , setters not shown */ } 

this approach doesn't work because jpa (hibernate in case) requires entity has id field. don't need unique id, since way table ever queried in select statement above.

note don't want store keywords in item entity - item should have no knowledge of indexing system.

i can envisage solutions involving new table of {item_id,score} tuples, wonder if it's possible using 1 table above?

thanks help!

the item_index table seems have composite id, consists of item_id , keyword. if understand correctly these pairs unique in table. indicates define primary key table , use composite id of entity.

take @ jpa composite primary key regarding definition of composite id. this article helpful.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -