1

I have two entity classes which don't contain any association in their mappings.

@Entity
@Table(schema = "mailing", name = "recipient_code_statistics")
public class RecipientCodeStatistics {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "recipient_code_id", unique = true)
    private long recipientCodeId;

    //GET, SET
}

and

@Entity
@Table(schema = "mailing", name = "recipient_code")
public class RecipientCode {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "code")
    private String code;

    //GET ,SET
}

Is it possible to write a criteria query, similar to the following:

SELECT * 
FROM recipient_code_statistic 
LEFT OUTER JOIN recipient_code
ON recipient_code_statistic.recipient_code_id = recipient_code.id
WHERE recipient_code.code = '123'

Whitout declaring association with @OneToMany annotation in the mapping explicitly?

user3663882
  • 5,799
  • 7
  • 40
  • 79

2 Answers2

1

It isn't possible to write an explicit join without mapping the two entity objects. But you could do a subquery.

Here is a reference to a similiar question.

And here the link to the answer containing a subquery example

Community
  • 1
  • 1
BeWu
  • 1,547
  • 1
  • 11
  • 20
1

try using a native query @NativeQuery()

Andre Coetzee
  • 1,060
  • 2
  • 19
  • 32