JPA @ManyToMany关系,@ JoinTable和@JoinColumn @OrderBy

时间:2013-04-30 15:44:56

标签: hibernate jpa sql-order-by jointable joincolumn

TABLE_A

  • ID
  • SORT

表-B

  • ID

使用这些表ManyToMany关联

有一个REF表

TABLE_REF

  • A_ID
  • B_ID

我使用B.getAs()

获取数据

我想获得As OrderBy TABLE_A.SORT字段。为此,我尝试添加OrderBy,但它无法访问B更近A的ORDER字段。

这就是为什么,我想如果我也可以将SORT字段加入REF表,我可以在B的关系图中使用@OrderBy(clause =" ORDER")参数。

但是几个小时我找不到任何解决方案。

以下是我用于映射的代码:

TABLE_A:

@ManyToMany(fetch=FetchType.EAGER, targetEntity = Role.class, cascade={CascadeType.MERGE, CascadeType.PERSIST})
@Fetch(FetchMode.SUBSELECT)
@JoinTable(name = "NC_ADMIN_ROLE_PAGE_XREF", joinColumns =
        @JoinColumn(name = "PAGE_ID", referencedColumnName = "ID"), 
    inverseJoinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID", nullable = true))
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="ncAdminElements")
@BatchSize(size = 50)
private List<Role> allRoles = new ArrayList<Role>(10);

表-B:

@Column(name = "NAME")
private String name;
@ManyToMany(fetch=FetchType.EAGER, targetEntity = Page.class, cascade={CascadeType.MERGE, CascadeType.PERSIST})
@Fetch(FetchMode.SUBSELECT)
@JoinTable(name = "NC_ADMIN_ROLE_PAGE_XREF", joinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID"), 
    inverseJoinColumns = @JoinColumn(name = "PAGE_ID", referencedColumnName = "ID", nullable = true))
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="ncAdminElements")
@BatchSize(size = 50)
private List<Page> allPages = new ArrayList<Page>(10);

我真正想要的是,由TABLE_A.DISPLAY_ORDER排序的TABLE_B.getAllPages()

任何关于hibernate for dummies中关于MAPPING RELATIONS的做得好的消息来源对我来说都是合适的。

0 个答案:

没有答案