Hello everyone I am a beginner with Springboot and I need some help with the setup, somehow when I start the spring application it throws the following error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Property 'com.example.shopping.models.Account.orders' is not a collection and
may not be a '@OneToMany', '@ManyToMany', or '@ElementCollection'
The Account model is as follows:
package com.example.shopping.models;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
@Entity
public class Account {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="account_id")
private Long id;
public String name;
public String email;
@OneToMany()
public Iterable<Order> orders;
}
I also have the Order model as follows:
package com.example.shopping.models;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
@Entity
public class Order {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator="order_id")
private Long id;
@Id
public Long accountId;
@ManyToOne
public Account account;
@OneToMany()
public Iterable<ProductOrder> products;
}
This is the application.properties file:
## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5
## PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/shopping?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true
spring.datasource.username=postgres
spring.datasource.password=test
# create and drop table, good for testing, production set to none or comment it
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.generate-ddl=true
app.db.init.enabled=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
And this is the pom.xml file:
<?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>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>shopping</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>shopping</name>
<description>Shopping project for Spring Boot</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
</project>
I didn't create the database, I think it should be automatically created by Spring with the tables as well correct?
The error that you get tells you what you have to change.
Property 'com.example.shopping.models.Account.orders' is not a collection and may not be a '@OneToMany', '@ManyToMany', or '@ElementCollection'
To fix it you have to replace
@OneToMany
public Iterable<Order> orders;
To:
@OneToMany
public List<Order> orders;
And also
@OneToMany
public Iterable<ProductOrder> products;
To use List
instead of Iterable