'Column not found when running unit tests in Spring Boot
I have this problem when testing a JPA repository:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "CREATED_AT" not found; SQL statement:
insert into people_establishments (created_at, establishment_id, person_id, updated_at) values (?, ?, ?, ?) [42122-212]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:502)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:477)
at org.h2.message.DbException.get(DbException.java:223)
at org.h2.message.DbException.get(DbException.java:199)
at org.h2.table.Table.getColumn(Table.java:754)
at org.h2.command.Parser.parseColumn(Parser.java:1264)
at org.h2.command.Parser.parseColumnList(Parser.java:1249)
at org.h2.command.Parser.parseInsert(Parser.java:1650)
at org.h2.command.Parser.parsePrepared(Parser.java:814)
at org.h2.command.Parser.parse(Parser.java:691)
at org.h2.command.Parser.parse(Parser.java:661)
at org.h2.command.Parser.prepareCommand(Parser.java:568)
at org.h2.engine.SessionLocal.prepareLocal(SessionLocal.java:631)
at org.h2.engine.SessionLocal.prepareCommand(SessionLocal.java:554)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1116)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:92)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:1044)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:109)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)
... 139 more
But this column exist in this entity file:
@Builder
@Getter
@Setter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "people_establishments")
public class PersonEstablishment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "establishment_id")
private Integer establishmentId;
@Column(name = "person_id")
private Integer personId;
@Column(name = "created_at")
private OffsetDateTime createdAt;
@Column(name = "updated_at")
private OffsetDateTime updatedAt;
@PreUpdate
@PrePersist
public void updateDateFields() {
if (this.createdAt == null) {
this.createdAt = OffsetDateTime.now();
}
this.updatedAt = OffsetDateTime.now();
}
}
Test method:
@DataJpaTest
class PersonRepositoryTest extends EntitiesFactory {
// ...
@Autowired
protected PeopleEstablishmentsRepository peopleEstablishmentsRepository;
@AfterEach
void tearDown() {
peopleRepository.deleteAll();
}
@Test
void itShouldFindPersonByIdAndEstablishmentIdCreatedBy() {
// given
int createdBy = 1;
Establishment e = createEstablishment("Restaurant", createdBy);
Person p = createPerson("John", createdBy);
PersonEstablishment personEstablishment = PersonEstablishment.builder()
.personId(p.getId())
.establishmentId(e.getId())
.build();
// The error is happenig here
peopleEstablishmentsRepository.save(personEstablishment);
// when
Optional<Person> expected = peopleRepository.findByIdAndEstablishmentIdCreatedBy(p.getId(), e.getId(), createdBy);
// then
assertThat(expected).isPresent();
}
// ...
}
I am using h2 database to run the test, and I have other similar tables and they are working, only this one is failing, I tried to check the name if I have a typo, but the name is correct. Do you know what could be happening?
EDIT:
I found the error, the relation between Establishment and PeopleEstablishments wasn't set correctly. I deleted this relationship and it works:
public class Establishment {
...
@ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinTable(schema = "public", name = "people_establishments",
joinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "establishment_id", referencedColumnName = "id")
)
private List<Person> people;
...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|