Я строю соединение с базой данных, передаю его в createEntityFromResultSet
и передаю этому методу методы findById
в моих классах репозитория. Теперь моя проблема состоит в том, что после вызова первого из этих методов параметр соединения устанавливается на null
, когда метод возвращается. Между двумя строками: объект и объект null
, у меня нет видимого способа заставить его обратиться к нулевому значению, особенно если параметр соединения является окончательным (это также происходит, когда это не так), и * Насколько мне известно, метод 1005 * не может даже установить начальный connection
в ноль.
Мой метод Я создаю объект Lombok и вызываю findById
in:
protected Homework createEntityFromResultSet(ResultSet rs, final Connection connection) {
ensureNotNull(rs);
try {
CourseRepository cr = new CourseRepository();
StudentRepository str = new StudentRepository();
SubjectRepository sur = new SubjectRepository();
return Homework.builder()
.course(cr.findById(Long.parseLong(rs.getString(5)), connection).get()) // Connection Object exists and works here
.creatingStudent(str.findById(rs.getLong(6), connection).get()) // Connection is already null here
.subject(sur.findById(rs.getLong(7), connection).get())
.id(rs.getLong(8))
.version(rs.getInt(9))
.build();
} catch (SQLException e) {
throw PersistenceException.forDatabaseFailure(e);
} catch (NoSuchElementException e) {
throw PersistenceException.forForeignKeyConstraint();
}
}
Мой findById
метод:
public Optional<ENTITY> findById(PK id, final Connection connection) {
ensureNotNull(id);
try (Connection con = (connection != null ? connection : getConnection())) {
if (stmtFindById == null) stmtFindById = con.prepareStatement(SQL_FIND_BY_ID);
stmtFindById.setLong(1, id.longValue());
ResultSet rs = stmtFindById.executeQuery();
return rs.next() ? Optional.ofNullable(createEntityFromResultSet(rs, con)) : Optional.empty();
// ^ The debugger tells me here that connection is set to null when it returns on the first call
} catch (SQLException e) {
throw PersistenceException.forDatabaseFailure(e);
// ^ The second execution throws here, message java.sql.SQLException: Connection is null.
}
}