java-在Spring MVC中自动生成ID
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-在Spring MVC中自动生成ID,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3956字,纯文字阅读大概需要6分钟。
内容图文
我正在尝试将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中的表格
<form:form method="POST" action="newproduct" modelAttribute="product">
<table>
<tr>
<td><form:label path="description">Description</form:label></td>
<td><form:input path="description" /></td>
</tr>
<tr>
<td><form:label path="price">Price</form:label></td>
<td><form:input path="price" /></td>
</tr>
<tr>
<td><form:label path="date">Date (dd/mm/yyyy)</form:label></td>
<td><form:input path="date" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
提交表单时出错
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,并将其用于表单处理或响应组装.
内容总结
以上是互联网集市为您收集整理的java-在Spring MVC中自动生成ID全部内容,希望文章能够帮你解决java-在Spring MVC中自动生成ID所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。