Home > Software design >  Spring boot enity
Spring boot enity

Time:10-02

spring main class

  package com.example.spring.jpadata;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
import org.springframework.context.ApplicationContext;

import com.example.spring.jpadata.dao.UserRepository;

@SpringBootApplication
public class JpaApplication {

    public static void main(String[] args) {

        ApplicationContext context = SpringApplication.run(JpaApplication.class, args);

        UserRepository userRepository = context.getBean(UserRepository.class);

        User user = new User();
        user.setName("XYZ");
        user.setCity("Delhi");
        
    }

}

My setCity shows --The method setCity(String) is undefined for the type SecurityProperties.User. However, it is already defined in user class which is under entity package. I am sharing the entity package user class code and will share the screen shot as well.

User class

    package com.example.spring.jpadata.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String name;

    private String city;

    private String status;

    public User(int id, String name, String city, String status) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.status = status;
    }

    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "User [id="   id   ", name="   name   ", city="   city   ", status="   status   "]";
    }

}

The picture which i have added shows the error in setCity and the class User in which i have defined the attributes, I have shared the code as well.

CodePudding user response:

Actually, you used two different classes though they are both called User. The first one is org.springframework.boot.autoconfigure.security.SecurityProperties.User while the other is com.example.spring.jpadata.entities.User.

CodePudding user response:

The import is wrong.

Import your defined User class instead of Spring security user class.

Remove this org.springframework.boot.autoconfigure.security.SecurityProperties.User and import your user class.

CodePudding user response:

In JpaApplication class change this:

import org.springframework.boot.autoconfigure.security.SecurityProperties.User;

to this:

import com.example.spring.jpadata.entities.User;
  • Related