How to query two entities by attributes which are not primary keys

29 views
Skip to first unread message

Georg Nozicka

unread,
Oct 9, 2016, 1:29:02 PM10/9/16
to Querydsl

I would like to query (using JPAQuery) two entities by one of their columns which are not primary keys.


I am using

Spring Boot 1.4.0.RELEASE

Hibernate 5.2.2.Final

Querydsl 4.1.3


Imagine Table A and Table B, each having an OID as primary key, a column key and another column value, each of type String.


I want to get all value fields of table A and B where the key (and not the oid) is equal.


In plain SQL I would write:


select * from A a

inner join B b

on a.key = b.key


How can I achieve this with JPA respectively querydsl? The goal is to have the entities, so that I can do further processing with them (i.e. I do not want to use native SQL; I would like to do it with JPAQuery). If this could be achieved by adding relations, this would be fine two, but the relations needs to work with key and not via the oids.


The entities look like this.


@Entity

public class TestA {


  @Id

  @Column(name = "oid", columnDefinition = "BIGINT NOT NULL")

  @GeneratedValue(strategy = GenerationType.AUTO)

  private Long oid;


  @Column(nullable = false)

  private String key;


  @Column(nullable = false)

  private String value;


  public TestA() {

  }


  public TestA(String key, String value) {

    this.key = key;

    this.value = value;

  }

}


Entity TestB looks the same, just another name.


PS: My scenario is a little bit different, but I wanted to make this description simple because in the end, that, what I am asking for is the final problem I have in my more complex scenario.

Georg Nozicka

unread,
Oct 9, 2016, 5:22:14 PM10/9/16
to Querydsl
I found a solution and will post it tomorrow in detail. The trick is to use a OneToMany relation with @JoinColumn(name="key", referencedColumnName="key").
Reply all
Reply to author
Forward
0 new messages