Home > Back-end >  JPA Mapping seem ok but junitTest still give the exception JdbcSQLIntegrityConstraintViolationExcept
JPA Mapping seem ok but junitTest still give the exception JdbcSQLIntegrityConstraintViolationExcept

Time:12-22

I've got the following exception

org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "CART_ID"; SQL statement ...

eventhough everything seem to mapped correctly and when I debug the value that supposed to be null is there.

Here a example of the code and test giving that exception:

@Entity
@Table(name = "ITEM")
@Setter
public class ItemEntity {
    
    //...
    private CartEntity cart;

    public ItemEntity() {}
    
    // getters and setters
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "cart_id", nullable = false, insertable = false, updatable = false)
    public CartEntity getCart()
    {
        return cart;
    }
}



@Entity
@Table(name="CART")
@Setter
public class CartEntity {

    //...
    private List<ItemEntity> items;

    // getters and setters
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "cart")
    @OrderColumn(name = "RANK")
    public List<ItemEntity> getItems()
    {
        return items;
    }
}

@Repository
public interface ItemEntityRepository
    extends JpaRepository<ItemEntity, Long>
{
}

Here the test that still give the null eventhough when debugging the value is there


@Execution(ExecutionMode.CONCURRENT)
@DataJpaTest(showSql = false)
class ItemEntityRepositoryTest
{
    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private ItemEntityRepository repository;
    
    private Long entityId;
        
    @BeforeEach
    void init()
    {
        Item item = new Item();
        // ... fill other item attributes here
    
        CartEntity cart = new CartEntity();
        // ... fill other cart attribute here

        Long idSet = (Long) entityManager.persistAndGetId(cart);
        cart.setId(idSet);
        cart.setItems(Collections.singletonList(item));

        item.setCompetitionConfigurationSet(cart);

        entityId = (Long) entityManager.persistAndGetId(item);
    }


    @Test
    void findAll_existingItems_returnsAllItems()
    {
        List<Items> items = repository.findAll();
        assertEquals(1, items.size());
    }
}

Note that the test for getting the individual entity work fine.

CodePudding user response:

The problem is in insertable = false in getCart(). Hibernate can not insert cart_id value in item.

Some more details:

If you enable logging for SQL:

spring.jpa.properties.hibernate.show_sql=true
logging.level.org.hibernate.orm.jdbc.bind=trace

you'll get:

Hibernate: insert into cart (id) values (default)
Hibernate: insert into item (id) values (default)
2022-12-21T19:50:53.196 03:00  WARN 9892 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
2022-12-21T19:50:53.196 03:00 ERROR 9892 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column "CART_ID"

If you remove insertable = false, your code will work. SQL statement for item will be different:

Hibernate: insert into item (id, cart_id) values (default, ?)
  • Related