I’m having a small problem with my insert statements in Spring-data/jpa/hibernate/mysql/junit application. I created a small unit test to see if i can do a simple INSERT statement.
This is what i have:
@Test
public void testAddAssignment() {
Assignment assignment = new Assignment();
assignment.setCharacter("Z");
assignment.setType(1);
assignmentController.addAssignment(assignment);
assignment = assignmentController.findAssignmentById(16);
assertEquals(16, assignment.getId());
assertEquals(1, assignment.getType());
assertEquals("Z", assignment.getCharacter());
}
When i try to execute this I get the following error:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.byronvoorbach.domain.Assignment]; SQL [insert into Assignment (CHARACTER, TYPE) values (?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.byronvoorbach.domain.Assignment]
And this is my addAssignment(assigment);
@Transactional
public Assignment addAssignment(Assignment assignment) {
return repository.save(assignment);
}
And this is my repository:
@Transactional(readOnly = true)
public interface AssignmentRepository extends JpaRepository<Assignment, Long> {
public List<Assignment> findByType(int type);
}
Since i never wrote an sql query I’m unable to find where the query gets messed up.
Does anyone have an idea?
(Let me know if you need more source code or my .xml files)
EDIT :
Thanks to JB Nizet and fmucar I was able to fix this problem. I changed the column names and now it works!! Apperantly there are a few limitations to column naming which I wasn’t aware of.
Try using something other than
characterandtypeas column names, because these are probably reserved keywords in SQL.