Issue
So, I have 2 entities defined in my code, like this:
public class EntityA {
@ManyToOne(cascade = ALL, fetch = LAZY, optional = false)
@JoinColumn(name = "entity_b_id", nullable = false)
private EntityA entityA;
}
public class EntityB {
@Id
@SequenceGenerator(name = "seq_entity_b", sequenceName = "seq_entity_b", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_entity_b")
@Column(name = "id", unique = true, nullable = false)
private Long id;
[...other stuff...]
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_entity_b_external_id")
@SequenceGenerator(name = "seq_entity_b_external_id", sequenceName = "seq_entity_b_external_id", allocationSize = 1)
@Column(name = "external_id", nullable = false, unique = true)
private Long externalId;
}
Then I have a dao layer defined like this:
@Repository
public class EntityADao {
@PersistenceContext
private EntityManager entityManager;
public void save(EntityA entityA) {
entityManager.persist(entityA);
entityManager.flush();
}
}
When this code is triggered, I get this exception thrown though:
2021-02-18 13:40:21.434 ERROR 17530 --- [tp1239821079-39] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: null value in column "external_id" of relation "entity_b" violates not-null constraint
Detail: Failing row contains (111111, aaaaaaaa, Aaaaaa, 111111, AAAAAA, 1, VALUE, null).
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [external_id" of relation "entity_b]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Notice how the last value is null, that one should be the external_id
. When I turn on the sql logs for jpa, I do not see anywhere the seq_entity_b_external_id
called (I expected something like select next_val('seq_entity_b_external_id')
.
Can anyone help me solve this issue? Thanks in advance.
Solution
Thanks to @Christian Beikov answer, I got an hint. The error I did was to use multiple sequences on an entity, which is something you cannot do. I solved the issue following the solution in this post
Answered By - eager_to_learn
Answer Checked By - Gilberto Lyons (JavaFixing Admin)