I am taking a REST api that works in Spring and trying to get it working with Spring boot 2.7.3 java 11 This code works in Spring and I can make CRUD calls to the api, but I get the below error using the same entity class, db, service and dao in boot. The paths in the annotations are correct in that the files exist there. Using the bean actuator, I can see the user entity is not listed.
When I make a call to the endpoint, java throws a not an entity error
java.lang.IllegalArgumentException: Not an entity: class com.test.user.entity.User.
I've looked through several SO questions, but nothing has worked
@RestController
@RequestMapping("/users")
public class UserRestController {
@Autowired
private UserService userService;
@GetMapping(value="/get/", consumes = "application/json", produces = "application/json")
public User getName(@RequestBody User user) {
User result = userService.getName(user);
return result;
}
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDao;
private JSONObject obj;
@Override
@Transactional
public User getName(User user) {
String email = user.getEmail();
User result = userDao.getUser(email);
if (result != null) {
User response = new User(result.getFirstName(), result.getLastName());
return response;
} else {
throw new UserNotFoundException("User not found " email);
}
}
@Repository
public class UserDaoImpl implements UserDao{
@PersistenceContext
private EntityManager entityManager;
private Logger logger = Logger.getLogger(getClass().getName());
@Override
public User getUser(String email) {
Session session = entityManager.unwrap(Session.class);
try {
User user = new User();
CriteriaBuilder criteria = session.getCriteriaBuilder();
CriteriaQuery<User> cq = criteria.createQuery(User.class);
Root<User> root = cq.from(User.class);
Predicate predicate = criteria.equal(root.get("email"), email);
Query query = session.createQuery(cq.select(root).where(predicate));
user = (User)query.getSingleResult();
return user;
} catch (Exception e){
logger.error(e);
}
return null;
}
@Repository
public interface UserRepository extends CrudRepository<User, String>{
}
@SpringBootApplication(scanBasePackages = {"com.test.user"})
@EntityScan(basePackages = {"com.test.entity"})
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@NotNull
@Pattern(regexp="^(\\ \\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$", message="Phone number must match the following: xxx-xxx-xxxx.")
@Column(name="phone_number", unique=true)
private String phoneNumber;
@NotEmpty
@Email(message="Email address is not well-formed.")
@Column(name="email", unique=true)
private String email;
@NotNull
@Column(name="first_name")
@NotEmpty(message="First name is required.")
@Pattern(regexp="[A-Za-z]{0,45}", message="First name must contain up tp 45 alphabetic characters.")
private String firstName;
@NotNull
@NotEmpty(message="Last name is required.")
@Pattern(regexp="[A-Za-z]{0,45}", message="Last name must contain up tp 45 alphabetic characters.")
@Column(name="last_name")
private String lastName;
public User() {}
public User(
@NotNull @Pattern(regexp = "^(\\ \\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$", message = "Phone number must match the following: xxx-xxx-xxxx.") String phoneNumber,
@NotEmpty @Email(message = "Email address is not well-formed.") String email,
@NotNull @NotEmpty(message = "First name is required.") @Pattern(regexp = "[A-Za-z]{0,45}", message = "First name must contain up tp 45 alphabetic characters.") String firstName,
@NotNull @NotEmpty(message = "Last name is required.") @Pattern(regexp = "[A-Za-z]{0,45}", message = "Last name must contain up tp 45 alphabetic characters.") String lastName) {
super();
this.phoneNumber = phoneNumber;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public User(
@NotNull @NotEmpty(message = "First name is required.") @Pattern(regexp = "[A-Za-z]{0,45}", message = "First name must contain up tp 45 alphabetic characters.") String firstName,
@NotNull @NotEmpty(message = "Last name is required.") @Pattern(regexp = "[A-Za-z]{0,45}", message = "Last name must contain up tp 45 alphabetic characters.") String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "User [id=" id ", phoneNumber=" phoneNumber ", email=" email ", firstName=" firstName
", lastName=" lastName "]";
}
}
CodePudding user response:
The entity package com.test.user.entity
is missing in your @EntityScan(basePackages = {"com.test.entity"})
annotation. Either fix the current base package or add the other base package to the annotation using
@EntityScan(basePackages = {"com.test.entity", "com.test.user.entity"})
When the entity package resides in the @SpringBootApplication(scanBasePackages = {"com.test.user"})
, the entity scan annotation can be removed.