当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;
(...)
}
我的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 :(得分:0)
您在该字段的一侧有一个Set。因此没有“排序”(除了hashCode()gves之外)。如果你想订购,请使用List(这是Java,真的与JPA无关)。
你似乎也错过了那个M-N的非所有者方面的“mappedBy”。
答案 1 :(得分:0)
@OrderBy
适用于ManyToMany。还有我在问题中提供的结构。问题是我的查询,JPA没有用它来管理。遗憾。