I have to classes, like this:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
private int id;
@Column(name = "first_name", unique = false, nullable = false)
private String firstName;
@Column(name = "last_name", unique = false, nullable = false)
private String lastName;
@Column(name = "user_name", unique = true, nullable = false)
private String username;
@Column(name = "pass_word", unique = true, nullable = false)
private String password;
@Column(name = "roles", unique = false, nullable = false)
private String roles;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Contact> contacts = new HashSet<Contact>();
}
and
@Entity
@Table(name = "contact")
public class Contact {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "contact_id", unique = true, nullable = false)
private Integer id;
@Column(name = "firstName", unique = false, nullable = false)
private String firstName;
@Column(name = "lastName", unique = false, nullable = false)
private String lastName;
@Column(name = "displayName", unique = false, nullable = false)
private String displayName;
@Column(name = "email", unique = false, nullable = false)
private String email;
@Column(name = "photoPath", unique = false, nullable = false)
private String photoPath;
@Column(name = "note", unique = false, nullable = false)
private String note;
@ManyToOne()
@JsonIgnore
@JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false)
private User user;
}
In application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/email_project
spring.datasource.username= root
spring.datasource.password= root
logging.level.org.hibernate.SQL= debug
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=create
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
In data.sql
INSERT INTO `email_project`.`user` ( first_name, last_name, user_name, pass_word, roles) VALUES ( 'a','a','a','a', 'ROLE_USER');
INSERT INTO `email_project`.`user` (first_name, last_name, user_name, pass_word, roles)VALUES ( 'b','b','b','b', 'ROLE_USER');
INSERT INTO `email_project`.`contact` (firstName, lastName, displayName, email, photoPath, note, user_id) VALUES ('a', 'a','a','a','a','a', 1)
When I tried inserting only users, that worked fine. But when I tried inserting contact, it fails. How can I fix this?
Here is some, reasonable, part of error message:
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #3 of URL [file:/F:/Workspaces_IntelliJ_Spring_Projects/MyProject/target/classes/data.sql]: INSERT INTO email_project.contact (firstName, lastName, displayName, email, photoPath, note, user_id) VALUES ('a', 'a','a','a','a','a', 1); nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'firstName' in 'field list'