spring data jpa for multiple joined table -
i have 2 tables: productusage , learner. productusage have field learner, learner has fields id , guid. need create query pull out productusage learner guid in specified user ids:
sql:
select * product_usage inner join learner on product_usage.learner_id = learner.id learner.guid in ("1234", "2345")
domain class:
@data @noargsconstructor @entity @table(name = "core_product_usage_increments") public class productusage { @id @generatedvalue(strategy = generationtype.auto) private integer id; @manytoone @joincolumn(name = "learner_id", nullable = false) private learner learner; @manytoone @joincolumn(name = "learning_language_id", nullable = false) private language language; } @data @noargsconstructor @entity @table(name = "learners") public class learner { @id @generatedvalue(strategy = generationtype.auto) private integer id; @column(name = "user_guid", nullable = false, unique = true) private string guid; @column(name = "first_name") private string firstname; @column(name = "last_name") private string lastname; }
and repository class:
@repository public interface productusagerepository extends crudrepository<productusage, integer> { @query("select p productusage p p.learnerguid = :learnerguid") list<productusage> findbylearnerguid(string learnerguid); }
client class call repository
@component public class myclient { @autowired private productusagerepository repository; public myclient(productusagerepository repository) { this.repository = repository; } public list<productusage> getproductuagebyleanrerguid(string learnerguid) { return repository.findbylearnerguid(learnerguid); } }
and test:
@test public void testclient() throws exception { myclient client = new myclient(repository); list<productusage> results = client.getproductuagebyleanrerguid("1234"); assertnotnull(result); }
and failed:
caused by: java.lang.illegalargumentexception: org.hibernate.queryexception: not resolve property: learnerguid of: com.acme.domain.spectrum.productusage [select p com.acme.domain.spectrum.productusage p p.learnerguid = :learnerguid] @ org.hibernate.ejb.abstractentitymanagerimpl.convert(abstractentitymanagerimpl.java:1364) @ org.hibernate.ejb.abstractentitymanagerimpl.convert(abstractentitymanagerimpl.java:1300) @ org.hibernate.ejb.abstractentitymanagerimpl.createquery(abstractentitymanagerimpl.java:294) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606)
it cannot recognize 'learnerguid' field in productusage, that's defined in learner class. how can perform query join multiple tables?
productusage has no learnerguid
property, learner
. try
@query("select p productusage p p.learner.guid = :learnerguid")
if doesn't work, have tip:
@query("select p productusage p join p.learner l l.guid = :learnerguid")
Comments
Post a Comment