0
    public class Customer implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idCustomer")
    private Integer idCustomer;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idCustomer")
    private Collection<Login> loginCollection;

    }

    public class Login implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idLogin")

    @JoinColumn(name = "idCustomer", referencedColumnName = "idCustomer")
    @ManyToOne(optional = false)
    private Customer idCustomer;

    }

    //trying to save the customer and login in the database  

        ArrayList<Login> logins = new ArrayList<Login>();
        Login log = new Login();
        log.setIdCustomer(cust);
        logins.add(log);
        cust.setLoginCollection(logins);
        cust = custRepo.save(cust);  //failed 

    //Login log = new Login();
    //log.setUName(user);
    //log.setPassword(pass);
    //log.setIdCustomer(cust);
    //cust = custRepo.save(cust);
    //logRepository.save(log);  //failed too.

I'm using spring data in my project. I have 2 model classes Customer and Login. My login class has a foreign key idCustomer in the database. When I try to save the customer without a login, it works fine but the problem is that I can't save a login object in the database.

I'm getting an error saying

Unknown column 'id_customer' in 'field list' the jpa entities are generated.

Here is an image actual database.

enter image description here

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Priyamal
  • 2,919
  • 2
  • 25
  • 52

2 Answers2

1

Too many idCustomers in your example.

Try the code below. I have changed a bit the annotated members @OneToMany and @ManyToOne, following the tips established here JPA JoinColumn vs mappedBy

Also I have included idLogin in Login class. I don't know if this was a typo in your code.

public class Customer implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "idCustomer")
     private Integer idCustomer;

     @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
     private Collection<Login> loginCollection;

}

public class Login implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idLogin")
    private Integer idLogin;

    @ManyToOne
    @JoinColumn(name="idCustomer", insertable=false, updatable=false)
    private Customer customer;

}
Community
  • 1
  • 1
RubioRic
  • 2,442
  • 4
  • 28
  • 35