Я пытаюсь выполнить HQL для отображения многих в многие в hibernate и получаю странную проблему:
Причина: java. sql .SQLSyntaxErrorException: Неизвестный columnallowedage1_. allowAgencies_organization_id 'in' on clause '
at com. mysql .cj.jdb c .exceptions.SQLError.createSQLException (SQLError. java: 120)
в com. mysql .cj.jdb c .exceptions.SQLError.createSQLException (SQLError. java: 97)
По сути, у меня есть две таблицы, Reservation_Reason_Type и Agency. Мне нужно сделать много-много до них. Я верю, что все делаю правильно, но когда я запускаю код, это вызывает у меня эту неизвестную проблему с колонкой. Не уверен, почему это происходит с помощью allowage1_allowedAgencies_organization_id. Он также включает имя переменной в сгенерированный HQL.
Ниже приведены сопоставленные классы:
Агентство. java
@Entity
@Table(name = "AGENCY")
public class Agency {
@Id
@Column(name="organization_id")
private int id;
@Column(name="name")
private String name;
@Column(name="acronym")
private String acronym;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "allowedAgencies")
private Collection<ReservationReasonType> allowedReservations = new ArrayList<ReservationReasonType>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAcronym() {
return acronym;
}
public void setAcronym(String acronym) {
this.acronym = acronym;
}
public Collection<ReservationReasonType> getAllowedReservations() {
return allowedReservations;
}
public void setAllowedReservations(Collection<ReservationReasonType> allowedReservations) {
this.allowedReservations = allowedReservations;
}
}
ReservationReasonType. java
@Entity
@Table(name = "RESERVATION_REASON_TYPE")
public class ReservationReasonType {
@Id
@Column(name = "reservation_reason_type_id")
private int id;
@Column(name = "description")
private String description;
@Column(name = "reservation_reason_type_code")
private String code;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name="RES_AGENCY", joinColumns={@JoinColumn(referencedColumnName="reservation_reason_type_id")}
, inverseJoinColumns={@JoinColumn(referencedColumnName="organization_id")})
private Collection<Agency> allowedAgencies = new ArrayList<Agency>();
public ReservationReasonType(int id, String description, String code) {
this.setId(id);
this.setDescription(description);
this.setCode(code);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Collection<Agency> getAllowedAgencies() {
return allowedAgencies;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name="RES_AGENCY", joinColumns={@JoinColumn(referencedColumnName="reservation_reason_type_id")}
, inverseJoinColumns={@JoinColumn(referencedColumnName="organization_id")})
//Set<Agency> allowedAgencies = new HashSet<Agency>();
public void setAllowedAgencies(Collection<Agency> allowedAgencies) {
this.allowedAgencies = allowedAgencies;
}
}
Ниже приведен запрос что я использую для HQL:
String query = "select rrt from ReservationReasonType as rrt join rrt.allowedAgencies age where age.acronym = :agencyAcronym)";
Ниже приведен сгенерированный HQL, который не будет работать из-за неизвестных столбцов:
Hibernate:
select
reservatio0_.reservation_reason_type_id as reservat1_2_,
reservatio0_.reservation_reason_type_code as reservat2_2_,
reservatio0_.description as descript3_2_
from
RESERVATION_REASON_TYPE reservatio0_
inner join
RES_AGENCY allowedage1_
on reservatio0_.reservation_reason_type_id=allowedage1_.allowedReservations_reservation_reason_type_id
inner join
AGENCY agency2_
on allowedage1_.allowedAgencies_organization_id=agency2_.organization_id
where
agency2_.acronym=?
Я дважды проверил, делаю ли я правильное отображение, и я думаю, что нет никаких проблем с этим. В одном я не уверен, что использую Hibernate 3.0, hibernate jpa 2.0 и mysql 8.0.19. Не уверен, что это может быть проблемой с этим?
Нужна помощь.
ОБНОВЛЕНИЕ:
Для временного решения я добавил следующие два столбца в таблицу RES_AGENCY и она работает, но это всего лишь биндайд. Все еще пытаюсь выяснить, что может быть правильным решением:
allowReservations_reservation_reason_type_id allowAgencies_organization_id