0

I am trying to get the integration tests of my app running (the app itself seems to be working). The issue seems to be related to how the ID column of a table is defined (I suspect that auto_increment is ignored).

The test fails with the following stacktrace:

jakarta.validation.ConstraintViolationException: Validation failed for classes [org.app.domain.Category] during persist time for groups [jakarta.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=id, rootBeanClass=class org.app.domain.Category, messageTemplate='{jakarta.validation.constraints.NotNull.message}'}
]
    at app//org.hibernate.boot.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:161)
    at app//org.hibernate.boot.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:84)
    at app//org.hibernate.action.internal.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:201)
    at app//org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:79)
    at app//org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:682)
    at app//org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:293)
    at app//org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:274)
    at app//org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:324)
    at app//org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:393)
    at app//org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:307)
    at app//org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:223)
    at app//org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:136)
    at app//org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:177)
    at app//org.hibernate.event.internal.DefaultPersistEventListener.persist(DefaultPersistEventListener.java:95)
    at app//org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:79)
    at app//org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:55)
    at app//org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
    at app//org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:761)
    at app//org.hibernate.internal.SessionImpl.persist(SessionImpl.java:745)
    at [email protected]/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at [email protected]/java.lang.reflect.Method.invoke(Method.java:580)
    at app//org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:320)
    at app/jdk.proxy3/jdk.proxy3.$Proxy278.persist(Unknown Source)
    at app//org.app.web.rest.CompetitionResourceIT.createEntity(CompetitionResourceIT.java:108)
    at app//org.app.web.rest.CompetitionResourceIT.initTest(CompetitionResourceIT.java:146)
    at [email protected]/java.lang.reflect.Method.invoke(Method.java:580)
    at [email protected]/java.util.ArrayList.forEach(ArrayList.java:1596)
    at [email protected]/java.util.ArrayList.forEach(ArrayList.java:1596)

The code causing it:

    Category category;
    if (TestUtil.findAll(em, Category.class).isEmpty()) {
      category = CategoryResourceIT.createEntity();
      em.persist(category);
      em.flush();
    } else {
      category = TestUtil.findAll(em, Category.class).get(0);
    }

This is how the table is defined in liquibase:

122     <createTable tableName="category">
123       <column autoIncrement="true" name="ID" type="INT">
124         <constraints nullable="false" primaryKey="true"/>
125       </column>
126       <column name="sex" type="VARCHAR(1)">
127         <constraints nullable="false"
128                      checkConstraint="CHECK (sex IN ('m', 'f'))"/>
129       </column>
130       <column name="nat" type="CHAR(3)">
131         <constraints nullable="false"/>
132       </column>
133       <column name="cat" type="VARCHAR(10)">
134          <constraints checkConstraint="CHECK (cat IN ('veterans', 'senior', 'junior', 'cadets')))"/>
135       </column>
136       <column name="arme" type="VARCHAR(10)">
137         <constraints nullable="false"
138                      checkConstraint="CHECK (arme IN ('foil', 'epee', 'sabre'))"/>
139       </column>
140       <column name="descr" type="VARCHAR(50)"/>
141       <column name="team" type="BOOLEAN"/>
142     </createTable>

And this is the Bean:

@Table(name = "category")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@org.springframework.data.elasticsearch.annotations.Document(indexName = "category")
@SuppressWarnings("common-java:DuplicatedBlocks")
public class Category implements Serializable {

  private static final long serialVersionUID = 1L;

  @NotNull
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id", nullable = false)
  private Integer id;

  @NotNull
  @Column(name = "sex", nullable = false)
  @Convert(converter = SexConverter.class)
  @org.springframework.data.elasticsearch.annotations.Field(type = org.springframework.data.elasticsearch.annotations.FieldType.Keyword)
  private Sex sex;

If I have omitted important code / information, please let me know.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.