Abhishek
Abhishek

Reputation: 2019

Unknown name value [] for enum class having multiple enums

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.

Upvotes: 0

Views: 1386

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36123

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;

Upvotes: 1

Related Questions