我的问题是我无法更新或插入已存在的记录。
我有更新的方法或在我的CompanyService中插入新记录
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;
}
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;
}
}
JSP PAGE
<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>
有人可以帮忙 PS:问题不是公司或用户表/实体。它是如何更新新记录:D