我使用小型Spring启动应用程序,在编译期间收到错误org.hsqldb.HsqlException: user lacks privilege or object not found: USER0_.NAME
。我之前有相同的错误,现在我将其重现为最少量的代码。这是项目结构,
错误由控制器的方法findAll()
触发,
@Controller
public class UserController {
@Autowired
private UserService userService;
@GetMapping(value = "/")
public String index() {
return "redirect:/users";
}
@GetMapping(value = "/users")
public String showAllUsers(Model model) {
// triggered by this line
model.addAttribute("users", userService.findAll());
return "list";
}
}
错误堆栈的重要部分是,
org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select user0_.id as id1_1_, user0_.name as name2_1_ from user user0_]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
......
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USER0_.NAME in statement [select user0_.id as id1_1_, user0_.name as name2_1_ from user user0_]
.........
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: USER0_.NAME
提供了实体类,
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
private String name;
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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
if (getId() != user.getId()) return false;
return getName().equals(user.getName());
}
@Override
public int hashCode() {
int result = (int) (getId() ^ (getId() >>> 32));
result = 31 * result + getName().hashCode();
return result;
}
}
提供的存储库目录中的接口
public interface UserRepository extends CrudRepository<User, Long> {
}
之前提供了用户控制器类。如果需要,我可以提供更多信息。我的意图是将所有用户值传递给JSP页面。我应该提一下,我现在没有任何用户价值,应用程序将从表单中获取用户信息,现在它什么都没有。
根据评论中的要求,提供了服务界面和classe,
public interface UserService {
List<User> findAll();
User findById(Long idx);
void save(User user);
void delete(Long idx);
}
服务类实现,
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public List<User> findAll() {
return (List)userRepository.findAll();
}
@Override
public User findById(Long idx) {
return userRepository.findOne(idx);
}
@Override
public void save(User user) {
userRepository.save(user);
}
@Override
public void delete(Long idx) {
userRepository.delete(idx);
}
}
我使用HSQL
数据库并提供了应用程序属性,
server.port=8081
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
spring.thymeleaf.cache=false
spring.application.name=Bootstrap Spring Boot
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.devtools.restart.additional-paths=.
security.basic.enabled=true
security.user.name=john
security.user.password=123
spring.datasource.url=jdbc:hsqldb:file:db/registration;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.username=testuser
spring.datasource.password=testpassword
server.error.path=/error
server.error.whitelabel.enabled=false
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the com.boot.registration.entity manager).
# Show or not log for each sql query
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto=create
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.messages.basename=validation
Update
很明显,服务接口的所有操作都不起作用并提供相同的错误集。例如,如果我把代码放在,
@GetMapping(value = "/users")
public String showAllUsers(Model model) {
User user = new User();
user.setId(1L);
user.setName("Berlin");
userService.save(user);
// model.addAttribute("users", userService.findAll());
return "list";
}
我仍然有同样的错误Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: USER0_.NAME
这里有什么问题?
答案 0 :(得分:0)
我已将JpaRepositoriesAutoConfiguration
排除在@SpringBootApplication
之外,这显然解决了这个问题。
@EnableTransactionManagement
@SpringBootApplication(scanBasePackages = {"com.boot"} , exclude = JpaRepositoriesAutoConfiguration.class)
public class WebApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(WebApplication.class, args);
}
}