java自动生成用户id_java-在Spring MVC中自动生成ID

我正在尝试将JSP页面收集的数据保存在数据库(Postgres)中.最初,我尝试在表单中手动插入任何值(包括id),并且将数据保存在数据库中没有问题.现在,我试图自动生成id值,但对于如何正确执行它有些困惑.

我的模特-产品

public class Product {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

private Long id;

private String description;

private double price;

@DateTimeFormat(pattern = "dd/MM/yyyy")

private Date date;

public Product() { }

public Product(Long id, String description, double price, Date date) {

this.id = id;

this.description = description;

this.price = price;

this.date = date;

}

//getters and setters

}

我的控制器-DBConnection

@Controller

public class DBConnection {

@Autowired

private ProductDao prDao;

@RequestMapping(value = "/newproduct", method = RequestMethod.GET)

public ModelAndView showForm() {

Product product = new Product();

return new ModelAndView("newproduct", "product", product);

}

@RequestMapping(value = "/newproduct", method = RequestMethod.POST)

public ModelAndView submitForm(@ModelAttribute("product") Product product) {

ModelAndView mav = new ModelAndView();

prDao.addProduct(product.getId(), product.getDescription(), product.getPrice(), product.getDate());

mav.setViewName("product");

mav.addObject("product", product);

return mav;

}

}

产品展示

public class ProductDaoImpl implements ProductDao {

private DataSource dataSource;

private JdbcTemplate jdbcTemplate;

@Override

public void setDataSource(DataSource ds) {

this.dataSource = ds;

this.jdbcTemplate = new JdbcTemplate(dataSource);

}

@Override

public void addProduct(Long id, String description, double price, Date date) {

jdbcTemplate.update("INSERT INTO products values(?, ?, ?, ?)", id, description, price, date);

}

}

我在newproduct.jsp中的表格

Description
Price
Date (dd/mm/yyyy)

提交表单时出错

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO products values(?, ?, ?, ?)]; ERROR: null values in column "id" violates not-null constraint

Detail: The row contains error (null, nnvfe, 10.00, 2010-10-10).; nested exception is org.postgresql.util.PSQLException: ERROR: null values in column "id" violates not-null constraint

Detail: The row contains error (null, nnvfe, 10.00, 2010-10-10).

我认为问题出在addProduct方法中:当我尝试获取它时,尚未创建id值.

如何实现自动生成的ID? JPA批注是执行此操作的正确方法吗?

解决方法:

Now I’m trying to auto generate the id values, but I’m a little bit

confused about how to do it properly

如果要自动生成ID,请不要提供ID,即使它为null.因此,从addProduect方法中删除ID:

@Override

public void addProduct(String description, double price, Date date) {

jdbcTemplate.update("INSERT INTO products values(?, ?, ?)", description, price, date);

}

另外,使您的id字段自动递增,如question所示.

或者,更改addProduct方法并使用EntityManager#persist方法:

@Override

public void addProduct(Product product) {

entityManager.persist(product);

}

关于错误:

ERROR: null values in column “id” violates not-null constraint

由于您的表单未从客户端获取id值,因此/ newproduct端点将具有一个产品,其id值为null:

@RequestMapping(value = "/newproduct", method = RequestMethod.POST)

public ModelAndView submitForm(@ModelAttribute("product") Product product) { ... }

然后,将此空值作为参数传递给addProduct方法:

prDao.addProduct(product.getId(), ...)

最后,addProduct将尝试将该空值保存为具有Non-Null约束的主键的值,因此会出现该错误.

同样,使用实体对象作为与客户端通信的方式,例如产品,不是一个好习惯.尝试定义一些辅助抽象,例如Forms或DTO,并将其用于表单处理或响应组装.

标签:postgresql,jpa,spring,java

来源: https://codeday.me/bug/20191118/2031323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值