1 Reply Latest reply: Apr 9, 2013 10:47 AM by cdelahun RSS

    JPQL constructor expression in select with enum

    1001541
      Hi Everyone:

      I am using Spring Framwork with Hibernate/JPA to write a HQL query. My problem is with my enum FeedType (Using the first constructor from FEED -> first parameter is a FeedType), I get an error that says: unexpected end of subtree and it points to the query shown below. When I choose the second constructor from the domain, everything works perfectly. Any suggestion is appreciated. My query is below:


      @Query("SELECT new YesIHaveMyPackageNameHere.Feed(YesIHaveMyPackageNameHere.FeedType.SLEEP, se.startDate,'sleeptracker', cast(sum(se.minutesAsleep) as big_decimal), '','asleep',p) FROM Person p JOIN p.sleepProfile sp JOIN sp.sleepEpisodes se WHERE p.id = :userId order by se.startDate desc group by se.startDate")
      public List<Feed> getFeedFromSleep(@Param("userId") Long userId, Pageable page);


      *//My Domain:*

      @Entity
      @Table(name="Feed")
      @Audited
      public class Feed {


      public Feed(FeedType type, Date date, String link, BigDecimal value,
      String app, String unitType, Person person) {
      super();
      this.type = type;
      this.date = date;
      this.link = link;
      this.value = value;
      this.app = app;
      this.unitType = unitType;
      this.person = person;
      }

      public Feed(Date date, String link, BigDecimal value,
      String app, String unitType, Person person) {
      super();
      this.date = date;
      this.link = link;
      this.value = value;
      this.app = app;
      this.unitType = unitType;
      this.person = person;
      }

      public Feed()
      {

      }

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name="Id")
      private Long id;

      @Column(name="type")
      @Enumerated(EnumType.STRING)
      private FeedType type;

      @Column(name="date")
      private Date date;

      @Column(name="link")
      private String link;

      @Column(name="value")
      private BigDecimal value;

      @Column(name="app")
      private String app;

      @Column(name="unitType")
      private String unitType;

      @ManyToOne
      private Person person;

      public BigDecimal getValue() {
      return value;
      }

      public void setValue(BigDecimal value) {
      this.value = value;
      }

      public String getUnitType() {
      return unitType;
      }

      public void setUnitType(String unitType) {
      this.unitType = unitType;
      }

      public String getApp() {
      return app;
      }

      public void setApp(String app) {
      this.app = app;
      }

      public FeedType getType() {
      return type;
      }

      public void setType(FeedType type) {
      this.type = type;
      }

      public Date getDate() {
      return date;
      }

      public void setDate(Date date) {
      this.date = date;
      }


      public String getLink() {
      return link;
      }

      public void setLink(String link) {
      this.link = link;
      }

      public Person getPerson() {
      return person;
      }

      public void setPerson(Person person) {
      this.person = person;
      }

      }

      *//Enum*

      public enum FeedType {

      SLEEP("sleep"),
      FITNESS("fitness"),
      NUTRITION("nutrition");

      private FeedType(String name) {
      this.name = name;
      }

      private final String name;

      public String toString() {
      return name;
      }

      public String getName(){
      return this.name;
      }

      }

      Edited by: 998538 on Apr 7, 2013 9:49 AM
        • 1. Re: JPQL constructor expression in select with enum
          cdelahun
          This is not JPA or even a JPQL query, so you may want to either try a pure JPQL query or post your problem on a product specific forum.

          That said, I do not believe passing in an enum to the database so that it can return the constant will work, as there is no mapping telling how to convert to/from the FeedType.SLEEP type. It would be provider specific, and you would likely get back the string or a integer type from the database, not your enum.

          Regards,
          Chris