java - JPA Join using arbitrary field (not primary key) -
i've got 2 entities want join using field have in common, called shared_id. field not primary key of either entity. shared_id unique - each hipster have unique shared_id.
the tables like:
hipster fixie ========= ======== id id shared_id shared_id
there onetomany relationship between hipsters , fixies. i've tried this:
@entity public class hipster { @id @column(name = "id") private integer id; @column(name = "shared_id") private integer sharedid; @onetomany(mappedby = "hipster") private list<fixie> fixies; } @entity public class fixie { @id @column(name = "id") private integer id; @manytoone @joincolumn(name = "shared_id", referencedcolumnname = "shared_id") private hipster hipster; } @repository public class hipsterdao { @persistencecontext private entitymanager entitymanager; public hipster gethipsterbysharedid(integer sharedid) { string querystring = "select h hipster h h.sharedid = :sharedid"; typedquery<hipster> query = entitymanager.createquery(querystring, hipster.class); query.setparameter("sharedid", sharedid); try { return query.getsingleresult(); } catch (persistenceexception e) { return null; } } }
now, dao gives me error:
java.lang.illegalargumentexception: can not set java.lang.integer field hipster.sharedid java.lang.integer
i think it's upset because sharedid field used in relation, rather being basic field. haven't included sharedid field in fixie entity, same result if do. how persuade run query me? need change query or entities?
Comments
Post a Comment