1

I wrote native query but I'm getting an error:

The column name covidSymptomId is not valid.

What's wrong?

There are table in mssql

Error picture

CovidSymptom.java

 @Data
 @AllArgsConstructor
 @NoArgsConstructor
 @Entity
 @Table(name="CovidSymptom")
 public class CovidSymptom {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "covidSymptomId")
private int id;

@ManyToOne
@JoinColumn(name = "covidId")
private Covid covidSymptom;

@Column(name = "symptom")
private String symptom;
}

CovidSymptomDao.java

@Query(nativeQuery = true,value = "Select symptom From CovidSymptom GROUP BY symptom order by count(covidSymptomId) desc")
List<CovidSymptom> getMost3SymptomOffCovid();
2
  • As per the question guide, please do not post images of code, data, error messages, etc. - copy or type the text into the question. Please reserve the use of images for diagrams or demonstrating rendering bugs, things that are impossible to describe accurately via text. Commented Dec 30, 2021 at 18:51
  • What value would you show for that column? You are aggregating by a different column, so either you select an aggregation such as COUNT or MIN, or you remove the association. Commented Dec 30, 2021 at 21:06

2 Answers 2

1

You need to include all columns that are mapped in your query. So:

Select covidSymptomId, symptom....
Sign up to request clarification or add additional context in comments.

2 Comments

Now I'm getting " Column 'CovidSymptom.covidSymptomId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." this error.
@Kadri That's because you are aggregating, and that column is not in the GROUP BY so it needs an aggregation function
1

I'm not sure why you're getting a column name problem, since your select query returns a list of "symptom"(String), whilst your method provides a list of "CovidSymptom" (Object).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.