java - One to Many projections using QueryDSL with Spring -
i'm trying use querydsl in spring boot , working fine until bumped problem i'm not finding answer. suppose have following structure:
+-------+ +----------+ +-------+ | foo | | foo_bar | | bar | +-------+ +----------+ +-------+ | id | -------->| foo_id | +- | id | | name | | bar_id |<------| | name | +-------+ | position | +-------+ +----------+
i'm trying use projections
querydsl manage service response, have classes defined like:
foo.class
@entity @table(name = "foo") public class foo{ @id @generatedvalue(strategy = generationtype.identity) private long id; @column(nullable = false, length = 100) private string name; // relations @onetomany(fetch = fetchtype.lazy, mappedby = "foo") private list<foobar> foobars; // getters , setters }
foobar.class
@entity @table(name = "foo_bar") public class albumview{ @id @generatedvalue(strategy = generationtype.identity) private long id; @column(nullable = false) private int position; @manytoone(cascade = cascadetype.persist, fetch = fetchtype.lazy) @joincolumn(name = "foo_id") private foo foo; @manytoone(cascade = cascadetype.persist, fetch = fetchtype.lazy) @joincolumn(name = "bar_id") private bar bar; // getters , setters }
bar.class
@entity @table(name = "bar") public class bar{ @id @generatedvalue(strategy = generationtype.identity) private long id; @column(nullable = false, length = 100) private string name; // relations @onetomany(fetch = fetchtype.lazy, mappedby = "bar") private list<foobar> foobars; // getters , setters }
now in controller i'm trying following code return foo
foobars
bar
id 1. query kind of works, because returns foo
's have bar
id equals 1, don't know how position
attribute inside relation table.
jpqlquery query = new jpaquery(entitymanager); qfoo foo = qfoo.foo; qfoobar foobar = qfoobar.foobar; return query.from(foo) .join(foo.foobars, foobar).on(foobar.bar.id.eq((long)1)) .list(projections.bean(foo.class, foo.id, foo.name, projections.bean(foobar.class, ??? // foo.foobars.position can't used because foobars array // foo.foobars.get(0).position can't used because throws nullpointerexception ).as("foobars") ) );
thanks in advance.
Comments
Post a Comment