我正在尝试编写以下本机SQL查询,但是遇到无法解决的错误。 我想写请求sql native ...
我需要您的帮助:)
AssignmentDao:
``@Repository
public interface AssignmentDao extends JpaRepository<Assignment, Integer> {
@Query(value ="SELECT project.PROJECT_NAME, contributor.FIRST_NAME, assignment.START_DATE, assignment.END_DATE from assignment join contributor on assignment.CONTRIBUTOR_id=contributor.id join project on assignment.PROJECT_ID_PROJECT=project.ID_PROJECT", nativeQuery=true)
List<Assignment> fetchAssignmentDataInnerJoin();
AssignmentServiceimpl:
@Service("assignmentService")
public class AssignmentServiceImpl implements AssignmentService {
@Resource
private AssignmentDao assignmentDao;
public List<Assignment> fetchAssignmentDataInnerJoin(){
List<Assignment> list = assignmentDao.fetchAssignmentDataInnerJoin();
return list;
AppController:
@GetMapping({"/listAssignments"})
public String listAssignment(ModelMap model) {
List<Assignment> assignments = assignmentService.fetchAssignmentDataInnerJoin();
model.addAttribute("assignments", assignments);
System.out.println("Liste des affectations : " + assignments);
return "allAssignments";
}
application.properties:
# Configurations H2
spring.jpa.show-sql=true
spring.h2.console.enabled=true
# Configuration MariaDB
spring.datasource.url=jdbc:mariadb://localhost:3308/dbmycapla
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.format_sql=true
#spring.jpa.hibernate.ddl-auto=create-drop
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
这是错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT project.PROJECT_NAME, contributor.FIRST_NAME, assignment.START_DATE, assignment.END_DATE from assignment join contributor on assignment.CONTRIBUTOR_id=contributor.id join project on assignment.PROJECT_ID_PROJECT=project.ID_PROJECT]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
Caused by: java.sql.SQLSyntaxErrorException: No such column: id_assignment
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
答案 0 :(得分:0)
在命令SHOW CREATE TABLE之后:
assignment | CREATE TABLE `assignment` (
`id_ASSIGNMENT` int(10) unsigned NOT NULL AUTO_INCREMENT,
`PROJECT_ID_PROJECT` int(10) unsigned NOT NULL,
`CONTRIBUTOR_id` int(10) unsigned NOT NULL,
`START_DATE` date NOT NULL,
`END_DATE` date NOT NULL,
`ASSIGNMENT_RATE` float NOT NULL DEFAULT 1,
`ACTIVITY` varchar(45) DEFAULT NULL COMMENT 'Texte libre',
`BILLABLE` tinyint(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id_ASSIGNMENT`,`PROJECT_ID_PROJECT`,`CONTRIBUTOR_id`,`START_DATE`,`END_DATE`),
KEY `ASSIGNMENT_FKIndex1` (`CONTRIBUTOR_id`),
KEY `ASSIGNMENT_FKIndex2` (`PROJECT_ID_PROJECT`),
CONSTRAINT `ASSIGNMENT_ibfk_1` FOREIGN KEY (`CONTRIBUTOR_id`) REFERENCES `contributor` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `ASSIGNMENT_ibfk_2` FOREIGN KEY (`PROJECT_ID_PROJECT`) REFERENCES `project` (`ID_PROJECT`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 |