1

In a micronaut project I have this entity:

@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "product_client_systems")
@DynamicUpdate
public class ProductClientSystem implements Serializable {

    @EmbeddedId
    private ProductClientSystemId primaryKey;

    @ManyToOne(fetch =FetchType.LAZY)
    @JoinColumn(name = "client_id")
    @MapsId("clientSystemId")
    private ClientSystem clientSystem;

    @ManyToOne(fetch =FetchType.LAZY)
    @JoinColumn(name = "product_id")
    @MapsId("articleItemId")
    private Product product;

    @Column(name = "active" )
    private Boolean active = true;

    @Column(name = "created_at", updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt = new Date();

This is the id:

@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class ProductClientSystemId implements Serializable {

    @Column(name = "product_id")
    private Long articleItemId;
    @Column(name = "client_id")
    private Long clientSystemId;

I have created a repo with a custom update so I can update more than one item:

@Repository
public interface ProductClientSystemRepository extends JpaRepository<ProductClientSystem, ProductClientSystemId> {

    public List<ProductClientSystem> findByProductInList(List<Product> list);

    @Query("update ProductClientSystem set updatedAt=:updatedAt WHERE product in :list")
    public void updateUpdatedAtByProductInList( Date updatedAt, List<Product> list);

    @Query("update ProductClientSystem o set o.active=:active WHERE o in :list")
    public void updateActive(List<ProductClientSystem> list, boolean active);
}

The problem happens when I run a test, the update doesn´t look to update nothing:

@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
public class RTest {
    @Test
    void testRandom(){
        productClientSystemRepository.updateActive(articleCSNewUpdates,false);

        articleCSNewUpdates = productClientSystemRepository.findAll();

        assertFalse(articleCSNewUpdates.get(0).getActive()); // HERE IS THE ERROR

    }

The most strange thing is that if I run the app and I test it against an endpoint it works. I mean, the error only happens in the test.

Any suggestion?

Regards

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rafael
  • 185
  • 1
  • 4
  • 14
  • What is in the articleCSNewUpdates list initially that you are updating, and how have you read them in initially? If they were read in using the same EntityManager used in the test, then the read-all query will just return those instances without refreshing the data from the database. – Chris Mar 07 '23 at 15:17
  • Yes, I read the articleCSNewUpdates list at the beginning of the test but I did this in some other tests and the list was used to refresh after some changes. The problem is related to the updates in the repo, if I change this update and do a single merge then it works. – Rafael Mar 10 '23 at 12:16
  • So it isn't a new/clean repository, and is likely using the same EntityManager instance/context under the covers for all your tests. The EntityManager needs to be treated like a unit of work, and closed out (or cleared) between tests, just like any transactions wrapping the test should be rolled back. – Chris Mar 10 '23 at 15:42

0 Answers0