0

I'm writing a simple Spring JPA test application and getting error: "The bean 'userRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.". I understand that it points on bean duplications but I'm really confused where the hell duplication is. Just turning on bean overriding is not good choise I guess. Please, help.

The app's structure is:

User class

package com.mot.platform.bean;

import org.springframework.data.annotation.Id;

import javax.persistence.*;

@Entity
@Table(name="plan_user")
public class User {

   @Id
   @Column(name = "id", nullable = false)
   private Long id;

   @Column(name = "name", length = 255, nullable = false)
   private String name;

   @Column(name = "password", length = 255, nullable = false)
   private String password;

   public Long getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

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

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }
}
'''

**UserRepository class**<br />
'''
package com.mot.platform.model;

import org.springframework.stereotype.Repository;
import com.mot.platform.bean.User;
import org.springframework.data.repository.CrudRepository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {
    //User findById(long id);
}

UserService class

package com.mot.platform.model;

import com.mot.platform.bean.User;

public interface UserService {

    public void addUser (String name, String password);
    public User getUserById (Long id);

}

UserServiceImpl class

UserServiceImpl

package com.mot.platform.model;

import org.springframework.stereotype.Service;

import com.mot.platform.bean.User;
import java.util.Optional;

@Service
public class UsrServiceImpl implements UserService {

  //  @Autowired
    private UserRepository rep;

    public void addUser(String name, String password) {
        User user = new User();
        user.setName("TestHiber");
        user.setPassword("ttt");
        rep.save(user);
    }

    public User getUserById(Long id) {
        Optional<User> optusr = rep.findById(id);
        User usr = optusr.isPresent() ? optusr.get() : null;
        return usr;
     }
}

UserRepository

package com.motivation.platform.model;

import org.springframework.stereotype.Repository;
import com.mot.platform.bean.User;
import org.springframework.data.repository.CrudRepository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {
  }

application.properties

# ===============================
# DATABASE
# ===============================
spring.datasource.driver-class-name= oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:BDSM
spring.datasource.username=XXX
spring.datasource.password=YYY
# ===============================
# JPA / HIBERNATE
# ===============================
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.Oracle9Dialect
spring.jpa.hibernate.ddl-auto=update

I don't have another files/classes besides attached here.

Dec0de
  • 321
  • 1
  • 11

0 Answers0