I'm trying to create a Named query with the inner join condition in the Entity class. However the application is failing to start with an exception.
Entity:
@Entity
@Table(name = "fooentry")
@NamedQuery(name="query1", query="SELECT foo.id, foo.name, foo.type, foo.action, foo.createdDateTime FROM FooEntity foo " +
"INNER JOIN (SELECT name, type, MAX(createdDateTime) AS recenttime FROM FooEntity GROUP BY name, type) recententry " +
"ON (foo.name = recententry.name AND foo.type = recententry.type) AND createdDateTime = recenttime AND isExported = false",
lockMode=PESSIMISTIC_WRITE)
public class FooEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
@NotNull
private String msisdn;
@Column(name = "type")
@NotNull
private String iccid;
@Column(name = "action")
@NotNull
private Long inventoryActionId;
@NotNull
@Column(name = "is_exported")
private Boolean isExported;
@Column(name = "created_date_time")
@NotNull
private LocalDateTime createdDateTime;
//setter and getter
}
Exception: Application is failing to start with the following exception.
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.HibernateException: Errors in named queries:
query1 failed because of: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 161
You can try query like below. It should return expected result
SELECT foo.id, foo.name, foo.type, foo.action, foo.createdDateTime
FROM FooEntity foo
WHERE
foo.isExported = false AND
EXISTS(
SELECT recententry.name, recententry.type, MAX(recententry.createdDateTime) AS recenttime
FROM FooEntity recententry
WHERE recententry.name = foo.name AND recententry.type = foo.type
GROUP BY recententry.name, recententry.type
HAVING recententry.recenttime = foo.createdDateTime
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.