Reputation: 83
I have a query which when running via MySQL workbench gives the result as expected.
select * from route_generations JOIN linked_customers on linked_customers.linked_customer_id = route_generations.customer_id;
I then have two entity/java classes as follows:
@Entity
@Table(name = "route_generations")
public class RouteGeneration {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String customerName;
@NotNull
private String numDevices;
@NotNull
private LocalDateTime startDae;
private LocalDateTime endDate;
@NotNull
private double latitude;
@NotNull
private double longitude;
@NotNull
private int userId;
private long linkedId;
@NotNull
private String customerId;
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getNumDevices() {
return numDevices;
}
public void setNumDevices(String numDevices) {
this.numDevices = numDevices;
}
public LocalDateTime getStartDae() {
return startDae;
}
public void setStartDae(LocalDateTime startDae) {
this.startDae = startDae;
}
public LocalDateTime getEndDate() {
return endDate;
}
public void setEndDate(LocalDateTime endDate) {
this.endDate = endDate;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public long getLinkedId() {
return linkedId;
}
public void setLinkedId(long linkedId) {
this.linkedId = linkedId;
}
Next class:
@Entity
@Table(name = "linked_customers")
public class KinesisLinkedCustomer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long linkedId;
@NotNull
private String linkedServiceId;
@NotNull
private String linkedCustomerId;
@NotNull
private String imei;
@NotNull
private long userId;
public long getLinkedId() {
return linkedId;
}
public void setLinkedId(long linkedId) {
this.linkedId = linkedId;
}
public String getLinkedServiceId() {
return linkedServiceId;
}
public void setLinkedServiceId(String linkedServiceId) {
this.linkedServiceId = linkedServiceId;
}
public String getLinkedCustomerId() {
return linkedCustomerId;
}
public void setLinkedCustomerId(String linkedCustomerId) {
this.linkedCustomerId = linkedCustomerId;
}
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
I then query the database by the following bespoke method in my JPARepository:
@Repository
public interface RouteGenerationRepository extends JpaRepository<RouteGeneration, Long> {
@Query("FROM route_generations JOIN linked_customers on linked_customers.linked_customer_id = route_generations.customer_id")
Page<RouteGeneration> getAllLinkedCustomers(Pageable pageable);
}
On application startup I then get the following error:
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: route_generations is not mapped [FROM route_generations JOIN linked_customers on linked_customers.linked_customer_id = route_generations.customer_id]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:729) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:104) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350) ~[spring-orm-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at com.sun.proxy.$Proxy125.createQuery(Unknown Source) ~[na:na]
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87) ~[spring-data-jpa-2.1.9.RELEASE.jar:2.1.9.RELEASE]
... 58 common frames omitted
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: route_generations is not mapped [FROM route_generations JOIN linked_customers on linked_customers.linked_customer_id = route_generations.customer_id]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:219) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:611) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:720) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
... 66 common frames omitted
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: route_generations is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3695) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3584) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:271) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
... 72 common frames omitted
I have looked at these answers:
Mapping JPA entity relationships ---- JPA ManyToOne entity mapping
However, after trying things suggested I am still getting the error. Do I need to use:
@OneToMany on the field linked_customer_id in type linked customers
Any help would greatly be appreciated!
Upvotes: 0
Views: 132
Reputation: 83
This was resolved by using the following query in my Repository class:
FROM RouteGeneration r JOIN KinesisLinkedCustomer kl on kl.linkedCustomerId = r.customerId
Always remember to use java field names rather than database column names for any future readers!!
Upvotes: 1