I have entity like this:
@Entity("account")
@Table("core_user")
public class User{
@Id
String id
@Column
String username;
@ElementCollection(fetch = FetchType.LAZY)
@MapKeyColumn(name = "name")
@Column(name = "value")
@CollectionTable(name = "core_user_extra", joinColumns = @JoinColumn(name = "accountId"))
Map<String, String> attributes = new HashMap<String, String>();
}
will generate table like this:
core_user(id,username); core_user_extra(user_id,name,value);
when search user use JPQL:
from account a join a.attributes m where key(m) = ?1 and value(m) = ?2
Hibernate generate wrong sql:
select
from
core_user genericuse0_ cross
join
core_user_extra attributes1_ cross
join
core_user_extra attributes2_
where
genericuse0_.id=attributes1_.accountId
and genericuse0_.id=attributes2_.accountId
and attributes1_.name=?
and (
select
attributes2_.value
from
core_user_extra attributes2_
where
genericuse0_.id=attributes2_.accountId
)=?
I think the right sql may be
select * from core_user a join core_user_extra b where a.id=b.accountId and b.name=? and b.value=?
And EclipseLink generate sql as I expected.
... FROM core_user_extra t0, core_user t1 WHERE (((t0.name = ?) AND (t0.value = ?)) AND (t0.accountId = t1.ID))
|