spring Boot中如何解决sql语法异常和Unknown column 'id'
How to Solve sql grammar exception and Unknown column 'id' in springBoot
**** customer Class Model*********
@Entity
@Table(name="customer")
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int idCustomer;
@Column(nullable=false, length=45)
private String name;
//bi-directional many-to-one association to Login
@OneToMany(mappedBy="customer")
private Set<Login> logins;
//bi-directional many-to-one association to Transaction
@OneToMany(mappedBy="customer")
private Set<Transaction> transactions;
public Customer() {
}
public int getIdCustomer() {
return this.idCustomer;
}
public void setIdCustomer(int idCustomer) {
this.idCustomer = idCustomer;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<Login> getLogins() {
return this.logins;
}
public void setLogins(Set<Login> logins) {
this.logins = logins;
}
public Login addLogin(Login login) {
getLogins().add(login);
login.setCustomer(this);
return login;
}
public Login removeLogin(Login login) {
getLogins().remove(login);
login.setCustomer(null);
return login;
}
}
****** 登录 Class 型号*********
@Entity
@Table(name="login")
@NamedQuery(name="Login.findAll", query="SELECT l FROM Login l")
public class Login implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int idLogin;
@Column(length=45)
private String password;
@Column(length=45)
private String uName;
//bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumn(name="idCustomer", nullable=false)
private Customer customer;
public Login() {
}
public int getIdLogin() {
return this.idLogin;
}
public void setIdLogin(int idLogin) {
this.idLogin = idLogin;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUName() {
return this.uName;
}
public void setUName(String uName) {
this.uName = uName;
}
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
我的控制器方法在数据库中保存登录和客户对象
@RequestMapping(value="/save",method=RequestMethod.POST)public StringsaveCust(
@ModelAttribute("cust")
@Valid Customer cust,
BindingResult result,
@RequestParam("pass") String pass ,
@RequestParam("user") String user){
if (result.hasErrors()) {
return "redirect:/customer";
}
cust = custRepo.save(cust);//using spring DAO to save the object
Login log = new Login(); //creaing a new login
log.setUName(user); //setting the user
log.setPassword(pass); //setting the pass
log.setCustomer(cust); //setting the customer
logRepository.save(log); //doesnt save the login . bt customer is saved
return "success";
}
我已经提交了我在我的 springBoot 应用程序中使用的两个模型 类。在我的数据库中有 2 个表 customer 和 login,其中 login customerID 是一个外键。
当我尝试将我的 2 类 保存在数据库中时,客户已保存
而且我的登录名没有保存。我收到一条错误消息
Unknown column 'id_customer' in 'field list'
我在项目中使用 thymeleaf,我在浏览器中看到的错误是
SQLGrammarException:
我想我在控制器方法中做错了什么。谁能解释一下如何解决这个问题。
idCustomer 的列名称是什么?您没有将 name="COLUMN_NAME"
放在 @Column
注释中,因此 Hibernate 将您的 camelCaseParameter
默认为名为 camel_case_parameter
的列。具体来说,假设 idCustomer
映射到名为 id_customer
.
的列
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false) // missing name="COLUMN_NAME"
private int idCustomer;
**** customer Class Model*********
@Entity
@Table(name="customer")
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int idCustomer;
@Column(nullable=false, length=45)
private String name;
//bi-directional many-to-one association to Login
@OneToMany(mappedBy="customer")
private Set<Login> logins;
//bi-directional many-to-one association to Transaction
@OneToMany(mappedBy="customer")
private Set<Transaction> transactions;
public Customer() {
}
public int getIdCustomer() {
return this.idCustomer;
}
public void setIdCustomer(int idCustomer) {
this.idCustomer = idCustomer;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<Login> getLogins() {
return this.logins;
}
public void setLogins(Set<Login> logins) {
this.logins = logins;
}
public Login addLogin(Login login) {
getLogins().add(login);
login.setCustomer(this);
return login;
}
public Login removeLogin(Login login) {
getLogins().remove(login);
login.setCustomer(null);
return login;
}
}
****** 登录 Class 型号*********
@Entity
@Table(name="login")
@NamedQuery(name="Login.findAll", query="SELECT l FROM Login l")
public class Login implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false)
private int idLogin;
@Column(length=45)
private String password;
@Column(length=45)
private String uName;
//bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumn(name="idCustomer", nullable=false)
private Customer customer;
public Login() {
}
public int getIdLogin() {
return this.idLogin;
}
public void setIdLogin(int idLogin) {
this.idLogin = idLogin;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUName() {
return this.uName;
}
public void setUName(String uName) {
this.uName = uName;
}
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
我的控制器方法在数据库中保存登录和客户对象
@RequestMapping(value="/save",method=RequestMethod.POST)public StringsaveCust(
@ModelAttribute("cust")
@Valid Customer cust,
BindingResult result,
@RequestParam("pass") String pass ,
@RequestParam("user") String user){
if (result.hasErrors()) {
return "redirect:/customer";
}
cust = custRepo.save(cust);//using spring DAO to save the object
Login log = new Login(); //creaing a new login
log.setUName(user); //setting the user
log.setPassword(pass); //setting the pass
log.setCustomer(cust); //setting the customer
logRepository.save(log); //doesnt save the login . bt customer is saved
return "success";
}
我已经提交了我在我的 springBoot 应用程序中使用的两个模型 类。在我的数据库中有 2 个表 customer 和 login,其中 login customerID 是一个外键。 当我尝试将我的 2 类 保存在数据库中时,客户已保存 而且我的登录名没有保存。我收到一条错误消息
Unknown column 'id_customer' in 'field list'
我在项目中使用 thymeleaf,我在浏览器中看到的错误是
SQLGrammarException:
我想我在控制器方法中做错了什么。谁能解释一下如何解决这个问题。
idCustomer 的列名称是什么?您没有将 name="COLUMN_NAME"
放在 @Column
注释中,因此 Hibernate 将您的 camelCaseParameter
默认为名为 camel_case_parameter
的列。具体来说,假设 idCustomer
映射到名为 id_customer
.
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(unique=true, nullable=false) // missing name="COLUMN_NAME"
private int idCustomer;