java - Spring JPA join tables with grouping -
java - Spring JPA join tables with grouping -
i'm trying info grouping database using spring jpa, classes :
employee.java
@entity @table(name = "employee") public class employee implements serializable{ @id @generatedvalue(strategy = generationtype.identity) private integer id; @column(nullable=false, unique=true) private string name; @type(type="org.jadira.usertype.dateandtime.joda.persistentlocaldatetime") @column(name="date_of_birth") private localdatetime dateofbirth; private double salary; @column(name="marital_status") private string maritalstatus; @datetimeformat(pattern="mm/dd/yyyy") @column(name="date_of_hire") @type(type="org.jadira.usertype.dateandtime.joda.persistentlocaldatetime") private localdatetime dateofhire; private string title; @manytoone() @joincolumn(name="dpartment_id") private section department; //security additions @column(nullable=false) private string password; @onetomany(fetch=fetchtype.eager, cascade=cascadetype.all) @joincolumn(name="employee_id") private set<role> roles = new hashset<>(); }
department.java
@entity @table(name = "department") public class section implements serializable{ @id @generatedvalue(strategy = generationtype.identity) private integer id; @column(nullable=false, unique=true) private string name; @onetomany(fetch=fetchtype.eager, cascade=cascadetype.all) @joincolumn(name="department_id") private set<employee> employees = new hashset<employee>(); @onetoone() @joincolumn(name="manager_id",nullable=true) private employee manager; }
i'm trying info sql query :
select dep.id, dep.name department_name , sum(emp.salary) total_salaries, count(emp.id) employee_count, dep.manager_id employee emp inner bring together section dep on emp.department_id = dep.id grouping dep.id
i have tried using
query= entitymanager .createquery("select new com.me.domain.departmentsummary( d, sum(e.salary), count(e.id) ) " + " section d inner bring together d.employees e grouping d.id "); list items = query.getresultlist();
but query.getresultlist()
reason crashes nullpointerexception
.
i'm not sure have done wrong or if there other way it.
thanks in advance.
update: test case callstack :
java.lang.nullpointerexception @ org.hibernate.engine.internal.statefulpersistencecontext.getloadedcollectionownerornull(statefulpersistencecontext.java:859) @ org.hibernate.event.spi.abstractcollectionevent.getloadedownerornull(abstractcollectionevent.java:75) @ org.hibernate.event.spi.initializecollectionevent.<init>(initializecollectionevent.java:36) @ org.hibernate.internal.sessionimpl.initializecollection(sessionimpl.java:1847) @ org.hibernate.collection.internal.abstractpersistentcollection$4.dowork(abstractpersistentcollection.java:549) @ org.hibernate.collection.internal.abstractpersistentcollection.withtemporarysessionifneeded(abstractpersistentcollection.java:234) @ org.hibernate.collection.internal.abstractpersistentcollection.initialize(abstractpersistentcollection.java:545) @ org.hibernate.collection.internal.abstractpersistentcollection.read(abstractpersistentcollection.java:124) @ org.hibernate.collection.internal.persistentset.hashcode(persistentset.java:428) @ com.me.entities.employee.hashcode(employee.java:27) @ java.util.hashmap.hash(hashmap.java:362) @ java.util.hashmap.put(hashmap.java:492) @ java.util.hashset.add(hashset.java:217) @ java.util.abstractcollection.addall(abstractcollection.java:342) @ org.hibernate.collection.internal.persistentset.endread(persistentset.java:346) @ org.hibernate.engine.loading.internal.collectionloadcontext.endloadingcollection(collectionloadcontext.java:243) @ org.hibernate.engine.loading.internal.collectionloadcontext.endloadingcollections(collectionloadcontext.java:233) @ org.hibernate.engine.loading.internal.collectionloadcontext.endloadingcollections(collectionloadcontext.java:209) @ org.hibernate.loader.loader.endcollectionload(loader.java:1150) @ org.hibernate.loader.loader.initializeentitiesandcollections(loader.java:1119) @ org.hibernate.loader.loader.processresultset(loader.java:964) @ org.hibernate.loader.loader.doquery(loader.java:911) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:342) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:312) @ org.hibernate.loader.loader.loadentity(loader.java:2121) @ org.hibernate.loader.entity.abstractentityloader.load(abstractentityloader.java:82) @ org.hibernate.loader.entity.abstractentityloader.load(abstractentityloader.java:72) @ org.hibernate.persister.entity.abstractentitypersister.load(abstractentitypersister.java:3927) @ org.hibernate.event.internal.defaultloadeventlistener.loadfromdatasource(defaultloadeventlistener.java:460) @ org.hibernate.event.internal.defaultloadeventlistener.doload(defaultloadeventlistener.java:429) @ org.hibernate.event.internal.defaultloadeventlistener.load(defaultloadeventlistener.java:206) @ org.hibernate.event.internal.defaultloadeventlistener.proxyorload(defaultloadeventlistener.java:262) @ org.hibernate.event.internal.defaultloadeventlistener.onload(defaultloadeventlistener.java:150) @ org.hibernate.internal.sessionimpl.fireload(sessionimpl.java:1092) @ org.hibernate.internal.sessionimpl.internalload(sessionimpl.java:1019) @ org.hibernate.type.entitytype.resolveidentifier(entitytype.java:672) @ org.hibernate.type.entitytype.resolve(entitytype.java:490) @ org.hibernate.type.entitytype.nullsafeget(entitytype.java:290) @ org.hibernate.loader.hql.queryloader.getresultrow(queryloader.java:446) @ org.hibernate.loader.hql.queryloader.getresultcolumnorrow(queryloader.java:429) @ org.hibernate.loader.loader.getrowfromresultset(loader.java:741) @ org.hibernate.loader.loader.processresultset(loader.java:943) @ org.hibernate.loader.loader.doquery(loader.java:911) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:342) @ org.hibernate.loader.loader.dolist(loader.java:2526) @ org.hibernate.loader.loader.dolist(loader.java:2512) @ org.hibernate.loader.loader.listignorequerycache(loader.java:2342) @ org.hibernate.loader.loader.list(loader.java:2337) @ org.hibernate.loader.hql.queryloader.list(queryloader.java:495) @ org.hibernate.hql.internal.ast.querytranslatorimpl.list(querytranslatorimpl.java:357) @ org.hibernate.engine.query.spi.hqlqueryplan.performlist(hqlqueryplan.java:195) @ org.hibernate.internal.sessionimpl.list(sessionimpl.java:1269) @ org.hibernate.internal.queryimpl.list(queryimpl.java:101) @ org.hibernate.ejb.queryimpl.getresultlist(queryimpl.java:268) @ com.me.services.departmentservice.getdepartmentsummaries(departmentservice.java:72) @ com.me.services.departmentservice$$fastclassbycglib$$fbe553f5.invoke(<generated>) @ org.springframework.cglib.proxy.methodproxy.invoke(methodproxy.java:204) @ org.springframework.aop.framework.cglibaopproxy$cglibmethodinvocation.invokejoinpoint(cglibaopproxy.java:713) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157) @ org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation(transactioninterceptor.java:98) @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:262) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:95) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy.java:646) @ com.me.services.departmentservice$$enhancerbycglib$$a1dbb849.getdepartmentsummaries(<generated>) @ com.me.departmentservicetest.getdepartmentsummaries(departmentservicetest.java:72) @ 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) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:47) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:44) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:17) @ org.springframework.test.context.junit4.statements.runbeforetestmethodcallbacks.evaluate(runbeforetestmethodcallbacks.java:74) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:83) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:72) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:232) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:89) @ org.junit.runners.parentrunner$3.run(parentrunner.java:238) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:63) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:236) @ org.junit.runners.parentrunner.access$000(parentrunner.java:53) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:229) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:71) @ org.junit.runners.parentrunner.run(parentrunner.java:309) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:175) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192)
according hibernate docs:
hibernate not expand grouped entity, cannot write grouping cat if properties of cat non-aggregated. have list non-aggregated properties explicitly.
so in case, you'd have include section properties in grouping clause.
java sql spring hibernate jpa
Comments
Post a Comment