我有以下两张table:
@Entity
@Table(name = "zip_codes_germany")
public class ZipCodeArea implements Serializable {
@Id
@Column(name="zip_code")
private String zipCode;
@Column(name = "city")
private String city;
@ManyToMany
private Set<TravelAgency> agencies;
和
@Entity
@Table(name = "travel_agencies")
public class TravelAgency implements Serializable {
@Id
@Column(name = "name")
private String name;
@Id
@Column(name = "city")
private String city;
@Column(name = "ceo")
private String ceo;
@ManyToMany
@JoinTable(name = "travel_agency_zip_codes", joinColumns = {
@JoinColumn(name = "city", referencedColumnName = "city"),
@JoinColumn(name = "name", referencedColumnName = "name") }, inverseJoinColumns = @JoinColumn(name = "zip_code"))
private Set<ZipCodeArea> zipCodeAreas;
充满了一些数据。其中旅行社具有来自列的复合键 name
以及 city
邮政编码德国有主键 zip_code
.
我的表中已有以下数据:
当加载旅行社“好人公司”时,我希望它包含一组 ZipCodeArea
属于“弗赖堡-伊姆布雷斯高”城市的元素。因此,应进行以下测试:
@Before
public void init() {
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
} catch (Exception e) {
logger.error("Failed to create hibernate session: ", e);
}
}
@Test
public void testManyToMany() {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<TravelAgency> cq = criteriaBuilder.createQuery(TravelAgency.class);
Root<TravelAgency> root = cq.from(TravelAgency.class);
cq.select(root).where(criteriaBuilder.like(root.get("name"), "Nice Guys Company"));
List<TravelAgency> result = session.createQuery(cq).getResultList();
assertFalse(result.isEmpty());
assertFalse(result.get(0).getZipCodeAreas().isEmpty());
}
问题是,我的多对多声明没有将城市Map到城市,因此第二个 assertFalse
方法失败。有办法解决这个问题吗?
我试过以下方法 TravelAgency
班
@ManyToMany
@JoinTable(name = "travel_agency_zip_codes", joinColumns =
@JoinColumn(name = "city", referencedColumnName = "city")
, inverseJoinColumns = @JoinColumn(name = "city", referencedColumnName = "city"))
private Set<ZipCodeArea> zipCodeAreas;
但这导致了以下例外情况:
aused by: org.hibernate.AnnotationException: referencedColumnNames(city) of ZipCodeArea.zipCodeAreas referencing TravelAgency not mapped to a single property
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:333)
暂无答案!
目前还没有任何答案,快来回答吧!