當JPA嘗試選擇ADMUSER實體我有SQL錯誤JPA @ManyToMany訂貨失敗?我ADMUSER實體:使用@OrderBy
@Entity
@Table(name = "ADM_USERS")
@SequenceGenerator(name = "ADM_USER_SEQ", sequenceName = "ADM_USER_SEQ", allocationSize = 1)
public class AdmUser implements EntityInt, Serializable {
private static final long serialVersionUID = 786L;
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ADM_USER_SEQ")
private Long id;
(...)
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinTable(name = "loc_locations_adm_users", joinColumns = @JoinColumn(name = "id_user", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name = "id_location"))
@OrderBy("name")
private Set<LocLocation> locations;
(...)
}
我LocLocation實體:
@Entity
@Table(name = "loc_locations", schema = "public")
@SequenceGenerator(name = "LOC_LOCATIONS_SEQ", sequenceName = "LOC_LOCATIONS_SEQ", allocationSize = 1)
public class LocLocation implements EntityInt, java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "LOC_LOCATIONS_SEQ")
private Long id;
@Column(nullable = false, unique = true, length = 200)
private String name;
(...)
@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY, mappedBy="locations")
private List<AdmUser> users;
}
而現在 - 當JPA嘗試選擇ADMUSER實體我有SQL錯誤。通過JPA生成的查詢看起來是:
select
admuser0_.id as id1_2_0_,
admuser0_.actived as actived2_2_0_,
admuser0_.admin as admin3_2_0_,
admuser0_.allow_ip as allow_ip4_2_0_,
admuser0_.created as created5_2_0_,
admuser0_.deleted as deleted6_2_0_,
admuser0_.id_domain as id_doma16_2_0_,
admuser0_.email as email7_2_0_,
admuser0_.language as language8_2_0_,
admuser0_.login as login9_2_0_,
admuser0_.name as name10_2_0_,
admuser0_.passwd as passwd11_2_0_,
admuser0_.phone as phone12_2_0_,
admuser0_.picture as picture13_2_0_,
admuser0_.surname as surname14_2_0_,
admuser0_.theme as theme15_2_0_,
locations1_.id_user as id_user1_2_1_,
loclocatio2_.id as id_locat2_6_1_,
loclocatio2_.id as id1_17_2_,
loclocatio2_.description as descript2_17_2_,
loclocatio2_.name as name3_17_2_
from
public.ADM_USERS admuser0_
left outer join
public.loc_locations_adm_users locations1_
on admuser0_.id=locations1_.id_user
left outer join
public.loc_locations loclocatio2_
on locations1_.id_location=loclocatio2_.id
where
admuser0_.id=1
order by
locations1_.name
由點locations1_.name
訂單,但應loclocatio2_.name
。我的實體有什麼問題嗎?
不知道這意味着什麼「這個問題是我的查詢」,你沒有提供問題的查詢 – 2014-10-09 09:08:04
我沒有提供查詢,因爲查詢沒有指向AdmUser實體,在選擇連接的數據時被JPA動態加載,後來我注意到在直接選擇AdmUser時 - OrderBy正常工作,對不起。 – robson 2014-10-09 12:16:59