I have this spring data jpa method:
@Transactional
@Modifying
@Query("update Trainee t set t.trainers = :trainers where t.username = :username")
void updateTrainersByUsername(@Param("username") String username, @Param("trainers") List<Trainer> trainers);
My entities, which related to this problem:
@Getter
@Setter
@Entity
@Table(name = "trainees")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Trainee extends User {
@Column(name = "date_of_birth")
private LocalDate dateOfBirth;
private String address;
@ManyToMany
@JoinTable(
name = "trainees_trainers",
joinColumns = @JoinColumn(name = "trainee_id"),
inverseJoinColumns = @JoinColumn(name = "trainer_id")
)
private List<Trainer> trainers;
@OneToMany(mappedBy = "trainee", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@OnDelete(action = OnDeleteAction.CASCADE)
private List<Training> trainings;
}
@Getter
@Setter
@Entity
@Table(name = "trainers")
@NoArgsConstructor
public class Trainer extends User {
@ManyToOne
@JoinColumn(name = "specialization")
@NotNull
private TrainingType trainingType;
@ManyToMany
@JoinTable(
name = "trainees_trainers",
joinColumns = @JoinColumn(name = "trainer_id"),
inverseJoinColumns = @JoinColumn(name = "trainee_id")
)
private List<Trainee> trainees;
}
@Getter
@Setter
@Entity
@Table(name = "trainees")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Trainee extends User {
@Column(name = "date_of_birth")
private LocalDate dateOfBirth;
private String address;
@ManyToMany
@JoinTable(
name = "trainees_trainers",
joinColumns = @JoinColumn(name = "trainee_id"),
inverseJoinColumns = @JoinColumn(name = "trainer_id")
)
private List<Trainer> trainers;
@OneToMany(mappedBy = "trainee", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@OnDelete(action = OnDeleteAction.CASCADE)
private List<Training> trainings;
}
My stack trace, which I got when I call my method:
Caused by: java.lang.NullPointerException: Cannot invoke "org.hibernate.sql.ast.tree.expression.Expression.getColumnReference()" because "pathSqlExpression" is null
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSetClause(BaseSqmToSqlAstConverter.java:994)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitUpdateStatement(BaseSqmToSqlAstConverter.java:837)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitUpdateStatement(BaseSqmToSqlAstConverter.java:440)
at org.hibernate.query.sqm.tree.update.SqmUpdateStatement.accept(SqmUpdateStatement.java:175)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.translate(BaseSqmToSqlAstConverter.java:776)
at org.hibernate.query.sqm.internal.SimpleUpdateQueryPlan.createUpdateTranslator(SimpleUpdateQueryPlan.java:114)
at org.hibernate.query.sqm.internal.SimpleUpdateQueryPlan.executeUpdate(SimpleUpdateQueryPlan.java:60)
at org.hibernate.query.sqm.internal.QuerySqmImpl.doExecuteUpdate(QuerySqmImpl.java:705)
at org.hibernate.query.sqm.internal.QuerySqmImpl.executeUpdate(QuerySqmImpl.java:675)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$ModifyingExecution.doExecute(JpaQueryExecution.java:266)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:92)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:149)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:137)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:170)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:158)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:385)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:249)
at jdk.proxy4/jdk.proxy4.$Proxy110.updateTrainersByUsername(Unknown Source)
Do I anything wrong ? Or I can’t update my Many-To-Many collelctions using update query ?