0

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'

1 Answer 1

1

I think sql might set your column names as "first_name" instead of "firstName" and so on. This might happen if something regarding the hibernate naming strategy is not working okay and the @Column is basically not setting the given name in the columns.

Firstly check your database and see how the columns are named in the contract table. If it is as I said above changing the column names from "firstName" to "first_name" inside the insert should work just fine.

If that solves the issue you could also try adding those this property value to solve the hibernate naming strategy:

spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.