i am new to Spring boot application. i have been getting this error every time I try running the codes. i use intellij IDEA as my eidtor.and I have tried the multiple solutions online but I couldn’t fix it. here are the errors
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-02 12:36:19.105 ERROR 14880 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: Server.accessingdata.Secondhand, at table: reserve, for columns: [org.hibernate.mapping.Column(secondhand)]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.12.jar:5.3.12]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.12.jar:5.3.12]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.12.jar:5.3.12]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) [spring-boot-2.5.6.jar:2.5.6]
at Server.Application.main(Application.java:21) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.5.6.jar:2.5.6]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: Server.accessingdata.Secondhand, at table: reserve, for columns: [org.hibernate.mapping.Column(secondhand)]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-5.3.12.jar:5.3.12]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.12.jar:5.3.12]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.12.jar:5.3.12]
... 21 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: Server.accessingdata.Secondhand, at table: reserve, for columns: [org.hibernate.mapping.Column(secondhand)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:512) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:479) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.mapping.Property.isValid(Property.java:231) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:627) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:359) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:307) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1498) ~[hibernate-core-5.6.1.Final.jar:5.6.1.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.12.jar:5.3.12]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.12.jar:5.3.12]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.12.jar:5.3.12]
... 25 common frames omitted
Process finished with exit code 0
this is my application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/4sshop?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql: true
this is my application class
package Server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
;
@SpringBootApplication(scanBasePackages={"Server.repository"})
@EnableJpaRepositories(basePackages = "Server.repository")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
this is my entity class
package Server.accessingdata;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="userID")
private String userID;
@Column(name="userName")
private String userName;
@Column(name="password")
private String password;
@Column(name="Level")
private Integer Level;
public User() {}
public User(String userID, String userName, String password, Integer Level) {
this.userID=userID;
this.userName=userName;
this.password=password;
this.Level=Level;
}
public String getuserid() {
return userID;
}
public void setuserID(String userID) {
this.userID = userID;
}
public String getusername() {
return userName;
}
public void setusername(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getlevel() {
return Level;
}
public void setlevel(Integer Level) {
this.Level = Level;
}
@Override
public String toString() {
return "User[userID="+ userID +", userName="+ userName +", password="+ password +", Level="+ Level +"]";
}
}
this is my repository class
package Server.repository;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import Server.accessingdata.User;
public interface UserRepo extends JpaRepository<User,Long> {
Optional<User> findByUsername(String userName);
List<User> findByuserID(String userID);
List<User> findByPassword(String password);
List<User> findBylevel(Integer Level);
}
and this is my controller class
package Server.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import Server.accessingdata.User;
import Server.repository.UserRepo;
@CrossOrigin(origins="http://localhost:8081")
@RestController
@RequestMapping("/api")
public class UserControl {
@Autowired
UserRepo userRepo;
@GetMapping("/users")
public ResponseEntity<List<User>>getAllUsers(@RequestParam(required=false) String userID){
try {
List<User> users= new ArrayList<User>();
if(userID==null)
userRepo.findAll().forEach(users::add);
else
userRepo.findByuserID(userID).forEach(users::add);
if(users.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(users, HttpStatus.OK);
}catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/users/{userName}")
public ResponseEntity<User>getUserByUsername(@PathVariable("userName")String userName){
Optional<User> UserData= userRepo.findByUsername(userName);
if(UserData.isPresent()) {
return new ResponseEntity<>(UserData.get(), HttpStatus.OK);
}else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody User user){
try {
User _user= userRepo.save(new User(user.getuserid(), user.getusername(), user.getPassword(), user.getlevel()));
return new ResponseEntity<>(_user, HttpStatus.CREATED);
}catch(Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/users{userName}")
public ResponseEntity<User> updateUser(@PathVariable("userName") String userName, @RequestBody User user){
Optional<User> UserData= userRepo.findByUsername(userName);
if(UserData.isPresent()) {
User _user= UserData.get();
_user.setusername(user.getusername());
_user.setuserID(user.getuserid());
_user.setPassword(user.getPassword());
_user.setlevel(user.getlevel());
return new ResponseEntity<>(userRepo.save(_user), HttpStatus.OK);
}else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
@DeleteMapping("/users")
public ResponseEntity<HttpStatus> deleteAllUsers(){
try {
userRepo.deleteAll();
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}catch(Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/users/Level")
public ResponseEntity<List<User>> findByLevel(){
try{
List<User> users= userRepo.findBylevel(null);
if(users.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}return new ResponseEntity<>(users, HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
And also I have added my 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.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>Server</groupId>
<artifactId>carServer</artifactId> <version>0.0.1-SNAPSHOT</version>
<name>4sserver</name>
<description>server for 4sshop</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</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-web</artifactId>
</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-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>