0

I am trying to insert data into the h2 database taking input from user.But primary key is getting inserted but the other is stored as null.
Here is my application.properties

spring.sql.init.platform==h2
spring.datasource.url=jdbc:h2:mem:preethi
spring.jpa.defer-datasource-initialization: true
spring.jpa.hibernate.ddl-auto=update



Here is Controller class AlienController.java

package com.preethi.springbootjpa.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.preethi.springbootjpa.model.Alien;
import com.preethi.springbootjpa.repo.AlienRepo;

@Controller
public class AlienController {
    @Autowired
    AlienRepo repo;
    @RequestMapping("/")
    public String home()
    {
        return "home.jsp";
    }
    
    @RequestMapping("/addAlien")
   public String addAlien( Alien alien)
   {
        repo.save(alien);
       return "home.jsp";
   }
}


Here is Alien.java

package com.preethi.springbootjpa.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import org.springframework.stereotype.Component;
@Component
@Entity
public class Alien {
    @Id
    private int aid;
    
    private String aname;
    public Alien()
    {
        
    }
    public Alien(int aid, String aname) {
        super();
        this.aid = aid;
        this.aname = aname;
    }
    public int getAid() {
        return aid;
    }
    public void setAid(int aid) {
        this.aid = aid;
    }
    public String getName() {
        return aname;
    }
    public void setName(String name) {
        this.aname = name;
    }
    @Override
    public String toString() {
        return "Alien [aid=" + aid + ", name=" + aname + "]";
    }

}

Here is AlienRepo.java

package com.preethi.springbootjpa.repo;

import org.springframework.data.repository.CrudRepository;

import com.preethi.springbootjpa.model.Alien;

public interface AlienRepo extends CrudRepository<Alien,Integer>{

    
    
}

Here is data.sql;

insert into alien values(101,'Preethi');

when I try to insert data from data.sql,it is getting inserted but when I try to insert data taking input from user, data is stored as null(except primary key).
Here is the table :

table

2
  • Best to post the sql ddl create statement rather than an image. Commented May 21, 2022 at 4:21
  • yes, there is no issue in creating a table.The issue is that except the primary key, remaining are stored as null Commented May 21, 2022 at 5:03

2 Answers 2

1

I got the same issue when i forgot to add the gettters and setters for an entity class.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you ... exactly what happend to me . make just getters and forget setters
0

Finally solved it, there is no problem with the code...It's just all the configuraton things that messed up. Just did everything from scratch and took care of build path and configurations. Solved..

3 Comments

Can you tell me exactly what you change in the configuration?
what do you change in the configuration?
I was using STS -Spring Tool Suite and I noticed that profile isn't enabled. That's the only problem.

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.