I have the following issue using EclipseLink.
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.4</version>
I have a UserEntity:
@Entity
@Table(name = TableNames.CC_Users)
public class UserEntity extends AbstractEntity {
@EmbeddedId
private UserId id;
....
@OneToOne (optional = true, orphanRemoval = true, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name = Columns.vccIdCN, referencedColumnName = Columns.vccIdCN),
@PrimaryKeyJoinColumn(name = Columns.userIdCN, referencedColumnName = Columns.userIdCN)
})
private ClientUserEntity clientUser;
...
}
Then I have the ClientUserEntity:
@Entity
@Table(name = TableNames.CC_Client_Users)
public class ClientUserEntity extends AbstractEntity {
@EmbeddedId
private UserId id;
...
}
UserId is an Embedable with 2 string fields: vccId and userId.
When I try to get the users that are not ClientUser using the following JPQL:
"SELECT us FROM " + UserEntity.class.getSimpleName() + " us LEFT JOIN us.clientUser cu WHERE us.id.vccId = :vccId AND cu IS NULL";
I see the following Query in the debugger:
[EL Fine]: sql: ... --Thread(Thread[main,5,main])--SELECT ... t1.language, ... FROM cc_user t1 LEFT OUTER JOIN cc_client_user t0 ON ((t0.user_id = t1.user_id) AND (t0.vcc_id = t1.vcc_id)) WHERE (((t1.vcc_id IS NULL) AND (t1.user_id IS NULL)) AND (t1.vcc_id = ?))
First problem is that JPA didn't generate a proper SQL query, it looks like a JPQL query instead of SQL. Second problem is that the WHERE conditions are wrong. ((t1.vcc_id IS NULL) AND (t1.user_id IS NULL)) those fields are the fields of the embedded id. I don't understand why JPA is adding that condition to the WHERE. Maybe it's an unsupported scenario ?
Something similar happens if I use a different JPQL:
"SELECT us FROM " + UserEntity.class.getSimpleName() + " us WHERE us.id.vccId = :vccId AND us.clientUser is null"
JPA generates:
SELECT ... FROM cc_user WHERE ((vcc_id = ?) AND ((vcc_id IS NULL) AND (user_id IS NULL)))
In this case, its an SQL, but the WHERE is still wrong. Looks like JPA doesn't support the embedded id scenario for IS NULL case.
I would expect an SQL like explained here.
Should I use Native Query in this case?