gpt4 book ai didi

hibernate - java.lang.IllegalArgumentException : Unknown entity in Spring + Hibernate Application 异常

转载 作者:行者123 更新时间:2023-12-05 09:18:12 25 4
gpt4 key购买 nike

我正在尝试使用 Hibernate 创建小型字符串启动应用程序。尝试保存对象(用户)时出现以下错误

java.lang.IllegalArgumentException: Unknown entity: com.prithvi.thoughtprocess.beans.dao.impl.User
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_51]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_51]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_51]

我在谷歌上搜索了这个但没有找到解决方案。下面是我的用户实体 -

package com.prithvi.thoughtprocess.beans.domain;

import java.io.Serializable;

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 implements Serializable {

/**
*
*/
private static final long serialVersionUID = 3367865397662004005L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Integer id;

@Column(name = "name")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

UserDAOImpl 类如下 -

package com.prithvi.thoughtprocess.beans.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.prithvi.thoughtprocess.beans.dao.UserDAO;
import com.prithvi.thoughtprocess.beans.domain.User;

@Repository
public class UserDAOImpl implements UserDAO{

@PersistenceContext
private EntityManager entityManager;

@Override
public <S extends User> S save(S entity) {
entityManager.persist(entity);
return null;
}}

应用程序.properties 文件

spring.datasource.url=jdbc:mysql://localhost:3306/bookbank
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

下面是包含主要方法的类 -

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.prithvi.thoughtprocess.dto.BookDto;

@ComponentScan(basePackages = {"com.prithvi.thoughtprocess.beans"})
@SpringBootApplication
public class Example {

public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}

}

Controller 类-

package com.prithvi.thoughtprocess.beans.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.prithvi.thoughtprocess.beans.service.UserService;
import com.prithvi.thoughtprocess.dto.UserDto;

@RestController
public class UserController {

@Autowired
private UserService userService;


@RequestMapping(value = "user/save", method = RequestMethod.POST)
String save(@RequestBody UserDto userDto) {
userService.saveUser(userDto);
return "User save successfully";
}

@RequestMapping(value = "user/list", method = RequestMethod.GET)
List<UserDto> list() {
return userService.listUser();
}

}

调用DAO层的服务类-

package com.prithvi.thoughtprocess.beans.service.impl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.prithvi.thoughtprocess.beans.dao.UserDAO;
import com.prithvi.thoughtprocess.beans.domain.User;
import com.prithvi.thoughtprocess.beans.service.UserService;
import com.prithvi.thoughtprocess.dto.UserDto;

@Service
@Transactional
public class UserServiceImpl implements UserService {

@Autowired
private UserDAO userDAO;

@Override

public void saveUser(UserDto userDto) {
userDAO.save(convertToEntity(userDto));
}

public User convertToEntity(UserDto userDto){
User user = new User();
user.setId(userDto.getId());
user.setName(userDto.getName());
return user;
}

public UserDto convertToDto(User user){
UserDto userDto = new UserDto();
userDto.setId(user.getId());
userDto.setName(user.getName());
return userDto;
}

@Override
public List<UserDto> listUser() {
//List<User> users = userDAO.list();
List<UserDto> userDtos = new ArrayList<>();
/*for (User user : users) {
userDtos.add(convertToDto(user));
}*/
return userDtos;
}

}

最佳答案

@EntityScan( basePackages = {"com.prithvi.thoughtprocess.beans"} 添加到您的应用程序类。这是 hibernate 扫描您的实体所必需的。

关于hibernate - java.lang.IllegalArgumentException : Unknown entity in Spring + Hibernate Application 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45191105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com