0
votes

I'm trying to set up a relationship between two different tables on my database using spring jpa. However, each time i try to do a @OnetoMany or @ManytoOne i get the error mentioned in the title.

Student :

    package EIC.com.example.Backend.Entity;
    import org.hibernate.annotations.Cascade;
    import javax.persistence.*;

    @Entity
    @Table(name = "etudiant")

    public class Student {
        @Id
        @Column(name="Apogee")
        private int id;
        @Column(name="Prenom")
        private String first_name;
        @Column(name="Nom")
        private String last_name;
        @Column(name="Classement")
        private int rank;
        @Column(name="Filiere")
        private String speciality;
        @ManyToOne
        @Column(name = "Offre")
        private Offer school; 
        /* GETTERS SETTERS CONSTRUCTORS */
    }

Offer :

    package EIC.com.example.Backend.Entity;
    import javax.persistence.*;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    @Entity
    public class Offer {
        @Id
        @Column(name = "ID_OFFRE")
        private int id;
        @Column(name = "Type")
        private int type; // 0 for DD 1 for MOBILITY
        @Column(name = "Etablissement")
        private String school;
        @Column(name = "Filiere_Concerne")
        private String target;
        @Column(name = "Nb_place")
        private int seats;
        @Column(name = "Delai")
        private Date limit;
        @OneToMany
        @JoinColumn(name = "Offre")
        private List<Student> students=new ArrayList<>();
        /* GETTERS SETTERS CONSTRUCTORS */
    }

Dependencies :

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.2.RELEASE EIC.com.example Backend 0.0.1-SNAPSHOT Backend EIC project with Spring Boot and Angular

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</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.data</groupId>
        <artifactId>spring-data-rest-hal-browser</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-core</artifactId>
    </dependency>

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

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

2
With this type of error, there is usually a "Caused by" down lower in the stack trace that will tell you a more specific reason why entityManagerFactory bean couldn't be created. Check it out, and you can post it hereMichael

2 Answers

0
votes

U cannot have @Column and @ManyToOne at once

Use @JoinColumn instead of @Column instead. Your Student class should look like:

@Entity
@Table(name = "etudiant")

    public class Student {
        @Id
        @Column(name="Apogee")
        private int id;
        @Column(name="Prenom")
        private String first_name;
        @Column(name="Nom")
        private String last_name;
        @Column(name="Classement")
        private int rank;
        @Column(name="Filiere")
        private String speciality;
        @ManyToOne
        @JoinColumn(name = "Offre")
        private Offer school; 
        /* GETTERS SETTERS CONSTRUCTORS */
    }
0
votes

I do not think the error you see is related to the configuration of the beans you have posted. Boot is creating this entityManager on your behalf and is not able to successfully.

Check this post - it may be the key.