Spring JPA CRUD存储库和更新记录 [英] spring JPA CRUD Repository and updating a record
问题描述
我有一个网页,允许用户设置管理/配置数据应用程序,我想将这些数据存储在单个数据库记录中。我有一个POJO来包含数据。我已经编写了一个保存这些数据的Spring MVC应用程序。
问题在于,每次用户保存数据(通过从网页执行POST) Spring应用程序在数据库中创建一条新记录。
我使用的是Repository,我的印象是每次我在Repository.save()它会更新现有记录的对象(如果有的话),否则创建一个新的记录,并根据主键标识记录。我找不到一个更新方法。
我已经尝试了几种解决此问题的方法,但它们仍然会创建额外的记录,失败时会出现重复的键错误或者仅显示不行。
此外,似乎每次启动网页或应用程序时,数据库中的所有数据都将被删除。
那有什么窍门?非常感谢...
以下是我的代码:
AdminFormController.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
@Controller
public class Admin_FormController
{
@Autowired
Private AdminDataRepository rep;
@RequestMapping(value =/ admin,method = RequestMethod.GET)
public String adminForm(Model model)
{
AdminData ad = new AdminData );
model.addAttribute(adminForm,ad);
ad = rep.findById(1L);
if(ad!= null)
ad.setId(1L);
返回adminForm;
$ b @RequestMapping(value =/ admin,method = RequestMethod.POST)
public String adminSubmit(@ModelAttribute AdminData ad,Model model)
{
// ad.setId(1L);;
rep.save(ad);
model.addAttribute(adminForm,ad);
返回adminForm;
}
}
AdminDataRepository.java
import org.springframework.data.repository.CrudRepository;
public interface AdminDataRepository扩展CrudRepository< AdminData,String>
{
AdminData findById(Long Id);
}
AdminData.java
import java.util.logging.Logger;
import javax.persistence。*;
@Entity
public class AdminData
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String useDates;
private String startDate;
private String endDate;
public String getUseDates()
{
return useDates;
}
public String getStartDate()
{
return startDate;
}
public String getEndDate()
{
return endDate;
}
public void setUseDates(String s)
{
Logger.getGlobal()。info(() - >UseDates:+ s);
useDates = s;
}
public void setStartDate(String s)
{
Logger.getGlobal()。info(() - >Start Date:+ s) ;
startDate = s;
}
public void setEndDate(String s)
{
Logger.getGlobal()。info(() - >End Date:+ s) ;
endDate = s;
}
}
你需要在请求之间存储对象。您的选项:
$ b
- 使用隐藏表单字段
- 从数据库重新读取它(在您的POST方法)
- 使用会话
<1>不方便,且不安全。 2不支持并发控制。 3是安全和正确的。
要实现#3,请在您的之前添加 @SessionAttributes(yourAttributeName)
控制器
。在 POST
方法中添加 SessionStatus
参数,并调用 sessionStatus.setComplete() code>当你完成它。
示例这里。
This is a question about a Spring Boot MVC application with Hibernate and PostgreSQL.
I have a web page that allows a user to set administrative / configuration data for an application, and I want to store this data in a single database record. I have a POJO to contain the data. I have coded up a Spring MVC app that persists this data.
The trouble is that each time the user saves the data (by doing a POST from the web page) the Spring application creates a new record in the database.
I'm using a Repository, and I was under the impression that each time I did a Repository.save() on the object it would update the existing record if there is one, otherwise create a new one and that it would identify the record based upon the primary key. I could not find an "update" method.
I have tried several ways around this issue but they either still make extra records, fail with a duplicate key error or just plain don't work.
Also, it seems that each time I start the web page or the application all the data in the database is removed.
So what's the trick? Thanks very much...
Here is my code:
AdminFormController.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
@Controller
public class Admin_FormController
{
@Autowired
private AdminDataRepository rep;
@RequestMapping(value="/admin", method=RequestMethod.GET)
public String adminForm(Model model)
{
AdminData ad = new AdminData();
model.addAttribute("adminForm", ad);
ad = rep.findById(1L);
if(ad != null)
ad.setId(1L);
return "adminForm";
}
@RequestMapping(value="/admin", method=RequestMethod.POST)
public String adminSubmit(@ModelAttribute AdminData ad, Model model)
{
// ad.setId(1L);;
rep.save(ad);
model.addAttribute("adminForm", ad);
return "adminForm";
}
}
AdminDataRepository.java
import org.springframework.data.repository.CrudRepository;
public interface AdminDataRepository extends CrudRepository<AdminData, String>
{
AdminData findById(Long Id);
}
AdminData.java
import java.util.logging.Logger;
import javax.persistence.*;
@Entity
public class AdminData
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String useDates;
private String startDate;
private String endDate;
public String getUseDates()
{
return useDates;
}
public String getStartDate()
{
return startDate;
}
public String getEndDate()
{
return endDate;
}
public void setUseDates(String s)
{
Logger.getGlobal().info(() -> "UseDates: " + s);
useDates = s;
}
public void setStartDate(String s)
{
Logger.getGlobal().info(() -> "Start Date: " + s);
startDate = s;
}
public void setEndDate(String s)
{
Logger.getGlobal().info(() -> "End Date: " + s);
endDate = s;
}
}
You need to store the object somewhere between requests. Your options:
- Using hidden form fields
- Re-read it from the database (in your POST method)
- Use session
1 is inconvenient, and not secure. 2 Doesn't support concurrency control. 3 is secure and correct.
To implement #3, add @SessionAttributes("yourAttributeName")
just before your controller
. Add a SessionStatus
parameter to your POST
method, and call sessionStatus.setComplete()
when you're done with it.
Example here.
这篇关于Spring JPA CRUD存储库和更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!