我已经尝试使用Spring Boot+Spring Data JPA更新实体很久了。我得到所有正确的观点回到我。我的编辑视图按ID向我返回正确的实体。一切进展顺利。直到我实际尝试保存/合并/持久化对象。每次我拿回一个有新ID的新实体。我只是不知道为什么。我看了网上的例子,还有你可能会参考的重复问题的链接。那么,在这些代码中,我在哪里犯了错误呢?
package demo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "ORDERS")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "ORDER_NAME")
private String name;
@Column(name = "ORDER_DESCRIPTION")
private String description;
@Column(name = "ORDER_CONTENT")
private String content;
public Order() {}
public Order(String name, String description, String content) {
this.name = name;
this.description = description;
this.content = content;
}
public String getContent() {
return content;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
public Integer getId() {
return this.id;
}
public void setContent(String content) {
this.content = content;
}
public void setDescription(String description) {
this.description = description;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Order other = (Order) obj;
if (content == null) {
if (other.content != null)
return false;
} else if (!content.equals(other.content))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((content == null) ? 0 : content.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public String toString() {
return "Order [id=" + id + ", name=" + name + ", description="
+ description + ", content=" + content + "]";
}
}
package demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface OrderRepository extends JpaRepository<Order, Integer> {
public Order findByName(String name);
}
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
@Service("customJpaService")
public class CustomJpaServiceImpl implements CustomJpaService{
@PersistenceContext
private EntityManager em;
@Transactional
public Order saveOrUpdateOrder(Order order) {
if (order.getId() == null) {
em.persist(order);
} else {
em.merge(order);
}
return order;
}
}
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
public class OrderController {
//refactor to service with
//logging features
@Autowired
OrderRepository orderRepo;
@Autowired
CustomJpaService customJpaService;
@RequestMapping(value="/orders", method=RequestMethod.GET)
public ModelAndView listOrders() {
List<Order> orders = orderRepo.findAll();
return new ModelAndView("orders", "orders", orders);
}
@RequestMapping(value="/orders/{id}", method=RequestMethod.GET)
public ModelAndView showOrder(@PathVariable Integer id, Order order) {
order = orderRepo.findOne(id);
return new ModelAndView("showOrder", "order", order);
}
@RequestMapping(value="/orders/edit/{id}", method=RequestMethod.GET)
public ModelAndView editForm(@PathVariable("id") Integer id) {
Order order = orderRepo.findOne(id);
return new ModelAndView("editOrder", "order", order);
}
@RequestMapping(value="/updateorder", method=RequestMethod.POST)
public String updateOrder(@ModelAttribute("order") Order order, BindingResult bindingResult, final RedirectAttributes redirectattributes) {
if (bindingResult.hasErrors()) {
return "redirect:/orders/edit/" + order.getId();
}
customJpaService.saveOrUpdateOrder(order);
redirectattributes.addFlashAttribute("successAddNewOrderMessage", "Order updated successfully!");
return "redirect:/orders/" + order.getId();
}
@RequestMapping(value="/orders/new", method=RequestMethod.GET)
public ModelAndView orderForm() {
return new ModelAndView("newOrder", "order", new Order());
}
@RequestMapping(value="/orders/new", method=RequestMethod.POST)
public String addOrder(Order order, final RedirectAttributes redirectAttributes) {
orderRepo.save(order);
redirectAttributes.addFlashAttribute("successAddNewOrderMessage", "Success! Order " + order.getName() + " added successfully!");
return "redirect:/orders/" + order.getId();
}
}
在这段代码之后。我的视图将我返回到正确的URL,但ID为4<--这是一个新的实体。更新的属性应该是3。
您需要将实体存储在GET请求和POST请求之间。您的选项:
3是唯一正确的解决方案,因为它允许乐观并发控制,并且比隐藏的表单变量更安全。
在控制器顶部添加@SessionAttributes(“ModelAttributeName”)
,并在POST处理程序方法中添加SessionStatus
参数。完成后调用SessionStatus.setComplete()
。参见Spring MVC:Validation,Post-Redirect-Get,Partial Updates,Optimic Concurrency,Field Security。
如何使用Spring Rest Controller和Spring Data JPA仅更新从@刚体传递的实体属性? 员工实体: 服务类方法: 请求体: Hibernate更新查询: Spring Data JPA正在尝试将company_id设置为空以进行更新,即使我没有将其传递给请求体?但是如果我从数据库中得到实体,使用employee_id传递,然后如果我试图保存(),那么它的工作正常。 我想
本文向大家介绍Oracle merge合并更新函数实例详解,包括了Oracle merge合并更新函数实例详解的使用技巧和注意事项,需要的朋友参考一下 前言 MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。 通过MERGE语句,根据一张表或多表联合查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,无法匹配的执行INSERT。这个语法仅需要一次
问题内容: 我在SQL Server中运行合并。在我的更新中,我只想更新值已更改的行。版本行在每次更新时都会递增。下面是一个示例: 现在,如果我只想更新行,从而增加版本,则仅在名称更改的情况下。 问题答案: 可以有。另外,无需更新。 如果Last_Name或First_Name可为空,则例如在比较trg.Last_Name <> src.Last_Name时,需要注意值。
问题内容: 有没有一种方法可以将两个主键合并为一个,然后级联更新所有受影响的关系?这是场景: 客户(idCustomer int PK,公司varchar(50)等) CustomerContacts(idCustomerContact int PK,idCustomer int FK,名称varchar(50)等) CustomerNotes(idCustomerNote int PK,idCu
首先,当你的手机上有应用程序,并且这个应用程序在play store上有更新时,google只返回更新的部分代码而不是整个包(例如3毫克而不是8毫克,这是整个包的大小)。我研究并发现,在android端,google play提取本地应用程序并合并从play store下载的更新文件,生成新的apk,然后安装新的apk和更新。假设play store没有apk keystore来签署新的apk,但