我正试图将数据插入h2数据库,从用户获取输入。但主键被插入,而另一个被存储为空。
这是我的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
这里是控制器类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 + "]";
}
}
这里是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>{
}
这里是data.sql;
insert into alien values(101,'Preethi');
当我尝试从data中插入数据时。sql,它正在插入,但当我试图插入数据从用户输入,数据存储为空(主键除外)。
表格如下:
表
当我忘记为实体类添加getter和setter时,我也遇到了同样的问题。
最后解决了,代码没有问题了…只是所有的构型都搞砸了。只是从头开始做所有事情,并负责构建路径和配置。解决. .