0

Dao Class:

@Entity @Table( name = "test" ) public class Test { @ManyToOne @JoinColumn( name = "college_id", referencedColumnName = "id" ) private College college; @Enumerated( EnumType.STRING ) @Column( name = "gender", length = 500 ) private Gender gender; @Enumerated( EnumType.STRING ) @Column( name = "section", length = 500 ) private Section section; ... } public enum Gender { Male, Female; } public enum Section { A(0), B(1), C(2); } 

RepositoryClass:

List<Test> findByGender( Gender gender ); List<Test> findBySection( Section section ); 

If I am calling findBySection(Section.A), it's working as expected.
But If I call findByGender(Gender.MALE), it's throwing error:

InvalidDataAccessAPIUsageException: Unknown name value [] for enum class [com. .. .constants.Section]

Strange part is If I am calling, findByCollegeAndGenderNot(College clg, Gender gender) its working.

1 Answer 1

1

I assume that you have null values in the column section in the database.

A data base column that you want to map to a Java Enum may not be nullable.

So you should check this and add a default value.

Additionally you should alter your column annotations to:

@Enumerated( EnumType.STRING ) @Column( name = "gender", length = 500, nullable = false ) private Gender gender; @Enumerated( EnumType.STRING ) @Column( name = "section", length = 500, nullable = false ) private Section section; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks much, yes there were a few nullable fields in both the columns

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.