【问题标题】:JPA @ManyToMany ordering failure using @OrderByJPA @ManyToMany 使用 @OrderBy 排序失败
【发布时间】:2014-12-02 20:27:51
【问题描述】:

当 JPA 尝试选择 AdmUser 实体时出现 sql 错误:

ERROR: column locations1_.name does not exist. 

我的实体有什么问题吗?我的 AdmUser 实体:

@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;
(...)
}

我的位置实体:

@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

order by 指向locations1_.name,但应该是loclocatio2_.name。我的实体有什么问题吗?

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    您在该字段的一侧有一个 Set。因此没有“排序”(除了 hashCode() 给出的)。如果您想订购,请使用 List(这是 Java,与 JPA 无关)。

    您似乎还缺少该 M-N 的非所有者方面的“mappedBy”。

    【讨论】:

    • 不是真的,因为在其他地方我有 Set 并且订购工作正常。问题是由 JPA 查询产生的,即使我尝试在 SQL 编辑器中执行 - 我也有同样的错误:错误:列位置 1_.name 不存在
    • 是的,我知道简单的 HashSet 没有排序。所以,我想知道为什么在其他情况下它是正确排序的。无论如何,即使更改为 List - 错误也是一样的。
    • 请注意我对答案的更新... mappedBy 不见了?此外,当它是 M-N 时,您只需要从一侧定义连接表
    • 这是不正确的 Hibernate 将在需要时使用 SortedSet 实现 stackoverflow.com/questions/2560590/hibernate-ordering-a-set
    • 有一个upvote,双向M-N关系需要mappedBy
    【解决方案2】:

    @OrderBy 可以很好地与多对多配合使用。还有我在问题中提供的结构。问题是我的查询,而 JPA 没有处理它。对不起。

    【讨论】:

    • 不知道“问题是我的查询”是什么意思。您没有在问题中提供查询。
    • 我没有提供查询,因为查询不指向 AdmUser 实体。它是由 JPA 在选择连接数据时动态加载的。我后来注意到,在直接选择 AdmUser 期间 - OrderBy 可以正常工作。对不起。
    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2017-10-09
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    相关资源
    最近更新 更多