**** 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;
}
}
****** Login Class Model*********
@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;
}
}
MY Controller Method saving login and customer object in the database
@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";
}
I have submitted the two model classes which im using in my springBoot application . In my database there are 2 tables customer and login where in login customerID is a foreign key. When i try to save my 2 classes in the database customer is saved and my login is not saved . i get an error saying
Unknown column 'id_customer' in 'field list'
i am using thymeleaf in my prohect and error i am seeing in the brower is
SQLGrammarException:
I think i am doing something wrong in the controller method. Can some one explain me how to solve this.