Home > Blockchain >  Getting an error while using String type as primary key. Error creating bean with name 'entityM
Getting an error while using String type as primary key. Error creating bean with name 'entityM

Time:10-18

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 org.hibernate.AnnotationException: No identifier specified for entity: com.hashir.smartcity.entities.User

I am using a String type field as my primary key. Here is my Entity package:

User.java:

package com.hashir.smartcity.entities;

import javax.persistence.Entity;

@Entity
public class User extends AbstractEntity{

    private String firstName;
    private String lastName;
    private String email;
    private String password;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    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;
    }

}

Wander.java:

package com.hashir.smartcity.entities;
        
        import javax.persistence.Entity;
        
        @Entity
        public class Wander extends AbstractEntity{
        
            private String placesToEat;
            private String visit;
            private String metro;
        
            public String getPlacesToEat() {
                return placesToEat;
            }
        
            public void setPlacesToEat(String placesToEat) {
                this.placesToEat = placesToEat;
            }
        
            public String getVisit() {
                return visit;
            }
        
            public void setVisit(String visit) {
                this.visit = visit;
            }
        
            public String getMetro() {
                return metro;
            }
        
            public void setMetro(String metro) {
                this.metro = metro;
            }
        
        }

AbstractEntity.java:

package com.hashir.smartcity.entities;

import javax.persistence.Id;

public class AbstractEntity {
    
    @Id
    private String location;

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

}

I want to register a user, provide a login and want my controller class to display menu as soon as the user enters location. Below is my Controller class:

UserController.java:

package com.hashir.smartcity.controllers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hashir.smartcity.entities.User;
import com.hashir.smartcity.repos.UserRepository;

@Controller
public class UserController {
    
    @Autowired
    UserRepository userRepository;
    
    
    @RequestMapping("/showReg")
    public String showRegistrationPage() {
        
        return "login/registerUser";
    }

    @RequestMapping(value="/registerUser",method=RequestMethod.POST)
    public String register(@ModelAttribute("user")User user) {
        userRepository.save(user);
        return "login/login";
    }
    
    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(@RequestParam("email")String email,@RequestParam("password")String password,ModelMap modelMap) {
        User user = userRepository.findByEmail(email);
        if(user.getPassword().equals(password)) {
            return "/enterLocation";
        }
        else {
            modelMap.addAttribute("msg", "Incorrect user name or Password. Please try again.");
        }
        return "login/login";
    }

        
        @RequestMapping(value="/enterLocation",method=RequestMethod.POST)
        public String enterLocation(@RequestParam("location")String location) {
            return "displayMenu";

    }
}

enterLocation.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Enter Location</title>
</head>
<body>
<h2>Enter Location</h2>
<form action="enterLocation" method="post">
Enter Location:<input type="text" name="location"/>
<input type="submit" value="search"/>

</form>
</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.5.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hashir.smartcity</groupId>
    <artifactId>smartcity</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>smartcity</name>
    <description>Smart City App</description>
    <properties>
        <java.version>1.8</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </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>

P.S. I am just a beginner.

CodePudding user response:

Using MappedSuperclass

@MappedSuperclass
public abstract class AbstractEntity  {
    // common mappings
    @Id
    private String location;

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

@Entity
@Table(name = "UserTable")
public class User  extends AbstractEntity {
 // other mappings
 
}

@Entity
@Table(name = "WanderTable")
public class WanderTableObject extends AbstractEntity {
    // other mappings
}

CodePudding user response:

As Navnath already mentioned the @Entity annotation expects an @Id notation on one of the members inside the class.

With what you are trying to do, using an abstract entity class to extend in all your entities, you can add the @Entity annotation on the abstract class.

  • Related