Java 如何在JPA Spring Boot中合并或生成UpdateorInsert
我的问题是,我无法更新或插入已存在的记录 我在我的公司服务中有更新或插入新记录的方法Java 如何在JPA Spring Boot中合并或生成UpdateorInsert,java,jsp,jpa,spring-boot,Java,Jsp,Jpa,Spring Boot,我的问题是,我无法更新或插入已存在的记录 我在我的公司服务中有更新或插入新记录的方法 public void save(Company company) { companyRepository.save(company); } 那是我公司的仓库 public interface CompanyRepository extends CrudRepository<Company, Integer> 那是我公司的桌子 当我尝试创建新唱片时,效果很好。但我不能更新新记录 完整
public void save(Company company) {
companyRepository.save(company);
}
那是我公司的仓库
public interface CompanyRepository extends CrudRepository<Company, Integer>
那是我公司的桌子
当我尝试创建新唱片时,效果很好。但我不能更新新记录
完整的公司实体
@PostMapping("/save-company")
public String saveTask(@ModelAttribute Company company, BindingResult bindingResult, HttpServletRequest request) {
companyservice.save(company);
return "createcompagny";
}
@Entity
public class Company {
private long companyId;
private String companyName;
private String website;
private String about;
private String city;
private int location;
private int industryid;
private int userid;
/**
* @return the userid
*/
public int getUserid() {
return userid;
}
/**
* @param userid the userid to set
*/
public void setUserid(int userid) {
this.userid = userid;
}
private int numbere;
private Industry industry;
private Countries countries;
private CompanySize companysize;
private User user;
/**
* @return the user
*/
@OneToOne(optional=false)
@JoinColumn(name = "userid",insertable=false,updatable=false)
public User getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(User user) {
this.user = user;
}
private Set<Job> job;
/**
* @return the job
*/
@OneToMany(mappedBy = "company", targetEntity = Job.class, cascade = CascadeType.ALL)
public Set<Job> getJob() {
return job;
}
/**
* @param job the job to set
*/
public void setJob(Set<Job> job) {
this.job = job;
}
/**
* @return the companysize
*/
@ManyToOne(optional = false)
@JoinColumn(name = "numbere", referencedColumnName = "id",insertable=false,updatable=false)
public CompanySize getCompanysize() {
return companysize;
}
/**
* @param companysize the companysize to set
*/
public void setCompanysize(CompanySize companysize) {
this.companysize = companysize;
}
/**
* @return the countries
*/
@ManyToOne(optional = false)
@JoinColumn(name = "location", referencedColumnName = "id",insertable=false,updatable=false)
public Countries getCountries() {
return countries;
}
/**
* @param countries the countries to set
*/
public void setCountries(Countries countries) {
this.countries = countries;
}
/**
* @return the industryid
*/
public int getIndustryid() {
return industryid;
}
/**
* @param industryid the industryid to set
*/
public void setIndustryid(int industryid) {
this.industryid = industryid;
}
/**
* @return the id
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getCompanyId() {
return companyId;
}
/**
* @param id
* the id to set
*/
public void setCompanyId(long id) {
this.companyId = id;
}
/**
* @return the name
*/
public String getCompanyName() {
return companyName;
}
/**
* @param name
* the name to set
*/
public void setCompanyName(String name) {
this.companyName = name;
}
/**
* @return the website
*/
public String getWebsite() {
return website;
}
/**
* @param website
* the website to set
*/
public void setWebsite(String website) {
this.website = website;
}
/**
* @return the about
*/
public String getAbout() {
return about;
}
/**
* @param about
* the about to set
*/
public void setAbout(String about) {
this.about = about;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city
* the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @param location
* the location to set
*/
public void setLocation(int location) {
this.location = location;
}
/**
* @return the industry_id
*/
public int getLocation() {
return location;
}
/**
* @param industry_id
* the industry_id to set
*/
/**
* @return the numbere
*/
public int getNumbere() {
return numbere;
}
/**
* @param numbere
* the numbere to set
*/
public void setNumbere(int numbere) {
this.numbere = numbere;
}
/**
* @return the industry
*/
@ManyToOne(optional = false)
@JoinColumn(name = "industryid", referencedColumnName = "id",insertable=false,updatable=false)
public Industry getIndustry() {
return industry;
}
/**
* @param industry
* the industry to set
*/
public void setIndustry(Industry industry) {
this.industry = industry;
}
// private byte[] logo;
}
@实体
公营公司{
私人长公司;
私有字符串companyName;
私人字符串网站;
私人串;
私人城市;
私密地点;
私营工业;
私有int用户id;
/**
*@返回用户ID
*/
public int getUserid(){
返回用户标识;
}
/**
*@param userid要设置的用户标识
*/
public void setUserid(int userid){
this.userid=userid;
}
私有整数;
私营工业;
私营国家;
私人公司化;
私人用户;
/**
*@返回用户
*/
@OneToOne(可选=错误)
@JoinColumn(name=“userid”,insertable=false,updateable=false)
公共用户getUser(){
返回用户;
}
/**
*@param user要设置的用户
*/
公共void setUser(用户){
this.user=用户;
}
私人工作;
/**
*@返回工作
*/
@OneToMany(mappedBy=“company”,targetEntity=Job.class,cascade=CascadeType.ALL)
公共集getJob(){
返回工作;
}
/**
*@param job要设置的作业
*/
公共无效设置作业(设置作业){
这个工作=工作;
}
/**
*@返回公司规模
*/
@多通(可选=假)
@JoinColumn(name=“numbere”,referencedColumnName=“id”,insertable=false,updateable=false)
上市公司规模getCompanysize(){
回归公司化;
}
/**
*@param companysize要设置的companysize
*/
public void setCompanysize(CompanySize CompanySize){
this.companysize=companysize;
}
/**
*@返回国家
*/
@多通(可选=假)
@JoinColumn(name=“location”,referencedColumnName=“id”,insertable=false,updateable=false)
公共国家/地区{
返回国;
}
/**
*@param countries要设置的国家/地区
*/
国家/地区(国家/地区){
这个国家=国家;
}
/**
*@returntheindustryid
*/
公共信息获取行业ID(){
回归工业;
}
/**
*@param industryid要设置的industryid
*/
公共空间设置行业ID(国际行业ID){
this.industryid=industryid;
}
/**
*@返回id
*/
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
公共长期公司ID(){
返回公司ID;
}
/**
*@param-id
*要设置的id
*/
public void setCompanyId(长id){
this.companyId=id;
}
/**
*@返回名称
*/
公共字符串getCompanyName(){
返回公司名称;
}
/**
*@param name
*要设置的名称
*/
public void setCompanyName(字符串名称){
this.companyName=名称;
}
/**
*@返回网站
*/
公共字符串getWebsite(){
返回网站;
}
/**
*@param网站
*要设置的网站
*/
公共网站(字符串网站){
this.website=网站;
}
/**
*@returntheabout
*/
公共字符串getAbout(){
返回大约;
}
/**
*@param关于
*天气就要放晴了
*/
公共无效设置(字符串设置){
这个;
}
/**
*@返回城市
*/
公共字符串getCity(){
回归城市;
}
/**
*@param市
*要设置的城市
*/
公共城市(字符串城市){
this.city=城市;
}
/**
*@param位置
*要设置的位置
*/
公共void设置位置(int位置){
这个位置=位置;
}
/**
*@return-the-industry\u-id
*/
公共int getLocation(){
返回位置;
}
/**
*@param-industry\u-id
*要设置的行业名称
*/
/**
*@返回号码
*/
public int getNumbere(){
返回编号;
}
/**
*@param数
*要设置的号码
*/
公共无效集合编号(整数编号){
this.numbere=numbere;
}
/**
*@回归行业
*/
@多通(可选=假)
@JoinColumn(name=“industryid”,referencedColumnName=“id”,insertable=false,updateable=false)
公共产业{
回归产业;
}
/**
*@param工业
*要设立的行业
*/
公共产业(工业){
这个。工业=工业;
}
//专用字节[]标识;
}
Complett用户实体
@Entity
@Table(name = "users")
public class User {
private Long id;
private String username;
private String password;
private String passwordConfirm;
private Set<Role> roles;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Transient
public String getPasswordConfirm() {
return passwordConfirm;
}
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}
@ManyToMany
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
@实体
@表(name=“users”)
公共类用户{
私人长id;
私有字符串用户名;
私有字符串密码;
私有字符串密码确认;
私人设定角色;
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getUsername(){
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
@短暂的
公共字符串getPasswordConfirm(){
返回密码确认;
}
public void setPasswordConfirm(字符串passwordConfirm){
这
<form method="POST" action="save-company">
<c:forEach var="user" items="${users}">
<c:if test="${pageContext.request.userPrincipal.name == user.username}">
<div class="row">
<div class="six columns">
<label for="usernameInput">Email Adresse</label>
<input class="u-full-width" type="text" placeholder="Email Adresse" id="usernameInput" name="username" value="${user.username}">
<input type="text" name="userid" value="${user.id}" />
</div>
</div>
</c:if>
</c:forEach>
<div class="row">
<div class="six columns">
<label for="compnaneInput">Company Name</label>
<input class="u-full-width" type="text" placeholder="Company Name" id="compnaneInput" name="companyName" value="${company.companyName}">
</div>
<div class="six columns">
<label for="countryInput">Country</label>
<select class="u-full-width" id="countryInput">
<option value="" disabled="disabled" selected="selected">Select the Country</option>
<c:forEach var="country" items="${countries}" >
<option id="countryID" value="${country.id}">${country.name}</option>
</c:forEach>
</select>
</div>
</div>
<div class="row">
<div class="six columns">
<label for="citynameInput">City Name</label>
<input class="u-full-width" type="text" placeholder="City Name" id="citynameInput" name="city" value="${company.city}">
</div>
<div class="six columns">
<label for="sizeInput">Size</label>
<select class="u-full-width" id="sizeInput">
<option value="" disabled="disabled" selected="selected">Select the Company Size (No. of employees)</option>
<c:forEach var="companySize" items="${companySize}">
<option value="${companySize.id}">${companySize.value}</option>
</c:forEach>
</select>
</div>
</div>
<div class="row">
<div class="six columns">
<label for="websiteInput">Website</label>
<input class="u-full-width" type="text" placeholder="Website" id="websiteInput" name="website" value="${company.website}">
</div>
<div class="six columns">
<label for="countryInput">Industry</label>
<select class="u-full-width" id="industryInput">
<option disabled="disabled" selected="selected">Select the Company Industry</option>
<c:forEach var="industry" items="${industry}">
<option value="${industry.id}">${industry.industryName}</option>
</c:forEach>
</select>
</div>
</div>
<label for="aboutus">About Us</label>
<textarea class="u-full-width" placeholder="About Us" id="about" name="about" value="${company.about}"></textarea>
<div class="row">
<div class="one columns">
<input class="u-full-width" type="text" placeholder="company Industry" id="companyIndustry" name="industryid" value="${company.industryid}">
</div>
<div class="one columns">
<input class="u-full-width" type="text" placeholder="company Location" id="companyLocation" name="location" value="${company.location}">
</div>
<div class="one columns">
<input class="u-full-width" type="text" placeholder="company Size" id="companySize" name="numbere" value="${company.numbere}">
</div>
</div>
<input class="button-primary" type="submit" value="SAVE">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>