DEV Community

Composite
Composite

Posted on

How to use SQLQueryFactory with your QueryDSL JPA Entities

Even you want QueryDSL SQL features such as bulk insert, you are using queryDSL JPA, unfortunately there is no way to generate RelationalPath<T> from EntityPath<T> automatically.

As @timo WestkΓ€mper feedback You can't turn it into a RelationalPath automatically

but you can create RelationalPathBase using your entityPath

RelationalPathBase relationalPathBase= new RelationalPathBase(this.entityPath.getType(), this.entityPath.getMetadata(), "yourSchemaName","yourTableName");

But there's a good news. you can wrap RelationalPath<T> from EntityPath<T> manually like this. so, I made util class that wrap RelationalPath<T> from EntityPath<T>.

TL;DR: copy the class below, apply it, and use it.

How to use? that's very simple. when you are using SQLQueryFactory, then just call QueryDslUtils.asRelational to wrap RelationalPath entity and use any SQLQueryFactory's clause.

@Entity
@Table(name = "my_table", schema = "my_schema") // MUST HAVE THIS ANNOTATION!
@Data // for Lombok
public class SomeEntity {
  @Column("some_col")
  private String someColumn;
  // ...getter, setter, etc.
}

EntityPath<SomeEntity> myEntity = QSomeEntity.someEntity;
RelationalPath<SomeEntity> sqlEntity = QueryDslUtils.asRelational(myEntity);

sqlQueryFactory.insert(sqlEntity)...execute();
Enter fullscreen mode Exit fullscreen mode

That's all. happy coding!

Top comments (0)