0

When I try to signup using form it shows error. The form exception shows "could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement". Is this some kind of hibernate error?

Here is my models:

Contact.java

@Entity
public class Contact {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int cid;
    private String name;
    private String secondName;
    private String work;
    private String email;
    private String phone;
    @Column(name = "image_url")
    private String imageUrl;
    @Column(length = 1000)
    private String description;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "contact_id", nullable = false)
    @JsonIgnore
    private User user;

    public Contact() {
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "Contact{" +
                "cid=" + cid +
                ", name='" + name + '\'' +
                ", secondName='" + secondName + '\'' +
                ", work='" + work + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", imageUrl='" + imageUrl + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

User.java

    @Entity
    public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int uid;
    private String name;
    @Column(unique = true)
    private String email;
    private String password;
    private String role;
    private boolean enabled;

    @Column(name = "image_url")
    private String imageUrl;
    @Column(length = 500)
    private String about;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
    private List<Contact> contacts = new ArrayList<>();

    public User() {
    }


    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public List<Contact> getContacts() {
        return contacts;
    }

    public void setContacts(List<Contact> contacts) {
        this.contacts = contacts;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                ", role='" + role + '\'' +
                ", enabled=" + enabled +
                ", imageUrl='" + imageUrl + '\'' +
                ", about='" + about + '\'' +
                '}';
    }
}

Signup.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="base::layout(~{::section})">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<section>
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-md-3">
                <div class="my-card mt-3">
                    <!--                            error message-->

                    <div th:if="${session.message}" th:classappend="${session.message.type}" class="alert" role="alert">
                        <p class="text-center" th:text="${session.message.content}"></p>
                        <th:block th:text="${#session.removeAttribute('message')}"></th:block>
                    </div>
                    <!--                            End of error message-->

                    <div class="container text-center">
                        <img style="width:90px" th:src="@{/image/membership.svg}"/>
                    </div>

                    <h1 class="text-center">Register Here!!</h1>

                    <form action="" th:action="@{/do_register}" method="post"
                          enctype="multipart/form-data" th:object="${user}">

                        <!--                                Name field-->
                        <div class="mb-3">
                            <label for="name_field" class="form-label">Your Name</label>
                            <input
                                    name="name"
                                    type="text"
                                    class="form-control"
                                    id="name_field"
                                    aria-describedby="emailHelp"
                                    placeholder="Enter Here"
                                    required="required"
                                    th:value="${user.name}">

                            <!--                                    <div class="invalid-feedback" th:each="e : ${#fields.errors('name')}"-->
                            <!--                                    th:text="${e}">-->

                            <!--                                    </div>-->
                        </div>

                        <!--                                Email field-->
                        <div class="mb-3">
                            <label for="email_field" class="form-label">Your Email</label>
                            <input
                                    name="email"
                                    type="email" class="form-control" id="email_field" aria-describedby="emailHelp"
                                    placeholder="Enter Here"
                                    required="required"
                                    th:value="${user.email}">
                        </div>

                        <!--                                Password field-->
                        <label for="password_field" class="form-label">Password</label>
                        <input
                                name="password"
                                type="password" id="password_field" class="form-control"
                                aria-describedby="passwordHelpBlock"
                                placeholder="Enter Here"
                                required="required">

                        <div id="passwordHelpBlock" class="form-text">
                            Your password must be 8-20 characters long, contain letters and numbers, and must not
                            contain spaces, special characters, or emoji.
                        </div>

                        <!--                                User about field-->
                        <div class="form-group my-3">
                                    <textarea
                                            name="about" th:text="${user.about}"
                                            th:value="${user.about}" id="" rows="8" class="form-control"
                                            placeholder="Write something about yourself"></textarea>
                        </div>

                        <!--                                User Image-->

                        <div class="custom-file my-3">
                            <input type="file" name="userImage">
                        </div>

                        <!--                                Terms and condition agreement-->
                        <div class="form-group form-check mb-2">
                            <input
                                    name="agreement"
                                    type="checkbox" class="form-check-input" id="agreement">
                            <label for="agreement">Accept terms and conditions</label>
                        </div>


                        <!--                                button field-->
                        <div class="container text-center d-grid gap-2">
                            <button type="submit" class="btn primarybackgroud text-white">Submit</button>

                            <button type="reset" class="btn bg-danger text-white">Reset</button>
                        </div>

                    </form>


                </div>
            </div>
        </div>
    </div>
</section>

</body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.smartmanager</groupId>
    <artifactId>Smart_Contact_Manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Smart Contact Manager</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Error messages in console :

2022-10-04 11:55:47.309 ERROR 14668 --- [nio-8282-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : Field 'uid' doesn't have a default value
org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:331)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
Sebastian
  • 5,721
  • 3
  • 43
  • 69

1 Answers1

0

Looks like you are not setting the @Id uid.

There are a lot of options, why, and also then how to fix them.

Is it an auto-incremented column? What kind of database are you using? Is the DB DDL generated (eg.: by hibernate) of is it something given? Eg.: mysql check that the column is auto-incremented, some other databases might need a trigger/sequence.

BTW, Identity is not the best of GenerationTypes. You could rather use SEQUENCE or AUTO.

Also, why are you specifying the joinColumn (IMHO, that is not required)? Also the column name contact_id looks suspicious, I would have expected user_id.

It could also be some issue with how the relationship is handled in services.

How do you assign the Contacts to a User? Are you both setting the Contact.user and also adding this to the User.contacts List? (Also, from the exception it is not clear, if the error occurs with User or Contact. Logging the sql might also help.)

TeeBea
  • 74
  • 7