java - Spring Data Jpa and Specification - how to work with ManyToOne and ManyToMany relations? -
i have simple model in project.
[updatepackage] >- (manytoone) - [version] -< [usecase] - (manytomany)
public class updatepackage implements comparable<updatepackage> { @manytoone(fetch = fetchtype.eager, cascade = cascadetype.all) @joincolumn(name = column_orig_version, nullable = true) private version origversion; // setters , getters } @entity @table(name = version.table_name) public class version { @manytomany(fetch = fetchtype.eager, cascade = cascadetype.all) @jointable(name = jvuc_table, joincolumns = { @joincolumn(name = jvuc_column_version, referencedcolumnname = column_id) }, inversejoincolumns = { @joincolumn(name = jvuc_column_usecase, referencedcolumnname = usecase.column_id) }) private final set<usecase> usecases = new hashset<usecase>(); // setters , getters } @entity @table(name = usecase.table_name) public class usecase { @column(name = column_name, nullable = false) private string name; // setters , getters }
for implementation of filter use spring data jpa , specification spring.data.jpa.domain
for instance find list of updatepackage given usecase names.
i understand manytoone relation need use join , manytomany need use fetch.
my implementation of specification interface looks this:
public static specification<updatepackage> usecasenames(final list<string> usecasenames) { return new specification<updatepackage>() { @override public predicate topredicate(root<updatepackage> root, criteriaquery<?> query, criteriabuilder cb) { final join<updatepackage, version> version = root.join(updatepackage_.destversion, jointype.left); fetch<version, usecase> usecase = version.fetch(version_.usecases, jointype.left); return null; // return usecase.get(usecase_.name).in(usecasenames); } }; }
when run integration test got npexception in line:
fetch<version, usecase> usecase = version.fetch(version_.usecases, jointype.left);
because fields joins , fetches of object version null.
i don't know in wrong way , cannot find answer in internet.
does know wrong in code?
stack: java.lang.nullpointerexception @ org.hibernate.ejb.criteria.path.abstractfromimpl.constructjoin(abstractfromimpl.java:261) @ org.hibernate.ejb.criteria.path.abstractfromimpl.fetch(abstractfromimpl.java:549)
i've found solution. had bug in static model.
static model from:
public static volatile singularattribute<version, usecase> usecases;
to:
public static volatile setattribute<version, usecase> usecases;
and in implementation of specification:
public static specification<updatepackage> usecasenames(final list<string> usecasenames) { return new specification<updatepackage>() { @override public predicate topredicate(root<updatepackage> root, criteriaquery<?> query, criteriabuilder cb) { final join<updatepackage, version> version = root.join(updatepackage_.destversion, jointype.left); final join<version, usecase> usecase = version.join(version_.usecases); return usecase.get(usecase_.name).in(usecasenames); } }; }
Comments
Post a Comment