2014-10-08 46 views
0

當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。我的實體有什麼問題嗎?

回答

0

@OrderBy很適合ManyToMany。還有我在我的問題中提供的結構。問題是我的查詢和JPA沒有管理它。抱歉。

+0

不知道這意味着什麼「這個問題是我的查詢」,你沒有提供問題的查詢 – 2014-10-09 09:08:04

+0

我沒有提供查詢,因爲查詢沒有指向AdmUser實體,在選擇連接的數據時被JPA動態加載,後來我注意到在直接選擇AdmUser時 - OrderBy正常工作,對不起。 – robson 2014-10-09 12:16:59

0

您有一個設置在該字段的一側。因此,沒有「排序」(除了hashCode()的含義)。如果您想要訂購,請使用List(這是Java,與JPA無關)。

您似乎也錯過了該M-N的非所有者一方的「mappedBy」。

+0

不是真的,因爲在其他地方,我已設置和訂購正常工作。問題是由JPA查詢生成的,即使我嘗試在SQL編輯器中執行 - 我有相同的錯誤:錯誤:列locations1_.name不存在 – robson 2014-10-08 09:54:39

+0

是的,我知道簡單的HashSet沒有排序。所以,我想知道爲什麼在其他情況下它是正確排序的。無論如何,即使更改爲列表 - 錯誤是一樣的。 – robson 2014-10-08 10:22:09

+0

請注意我對答案的更新... mappedBy缺失?你也只需要從一邊定義連接表,當它是一個MN – 2014-10-08 10:31:35