Home > Enterprise >  null value in column "id" of relation "technologies" violates not-null constrain
null value in column "id" of relation "technologies" violates not-null constrain

Time:11-27

Technology and technologyManager classes as shown below: Im trying to add a technology to database by swagger-ui but get an exception of null value in column "id" of relation "technologies" violates not-null constraint

public class Technology {

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

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

@ManyToOne(cascade = CascadeType.DETACH)
@JoinColumn(name = "language_id")
private ProgrammingLanguage language;
}
public void add(CreateTechnologyRequest technologyRequest) throws Exception {
    Technology technology = new Technology();
    
    
    if(technologyRequest.getName().isBlank()) {
        throw new Exception("Please enter a name");
    }else{
        for(Technology technologies : technologyRepository.findAll()) {
            if(technologies.getName().equalsIgnoreCase(technologyRequest.getName())) {
                throw new Exception("This name is already exist.");
            }else {
                
            }       technology.setName(technologyRequest.getName());    
                    
            for(ProgrammingLanguage language : languageRepository.findAll()) {
                
                if(language.getName().equalsIgnoreCase(technologyRequest.getLanguageName())) {
                    technology.setLanguage(language);
                }
            }           
        }
            technologyRepository.save(technology);
    }
    
}

I tried to add a new Technology to database by using swagger-ui but it threw an exception of null value in column "id" even though it is primary key and auto-generated.

CodePudding user response:

Try with GenerationType.AUTO instead of GenerationType.IDENTITY and change the type of the id from int to Integer or Long

  • Related