I am trying to run a JPA native query which will INSERT data in a postgres database.
However I am getting the following error :
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "myentity_pkey"
Detail: Key (id)=(0) already exists.
The entity Code looks like this :
@Data
@NoArgsConstructor
@Entity(name="my_entity")
public class MyEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column
private String value;
}
Repository code looks like this :
@Repository
public class MyEntityRepository {
@PersistenceContext
private EntityManager entityManager;
@Modifying
@Transactional
public void save(MyEntity me) {
Query query = entityManager.createNativeQuery("INSERT INTO my_entity (id, value) VALUES (?, ?)");
query.setParameter(1, me.getId());
query.setParameter(2, me.getValue());
query.executeUpdate();
}
}
Anyone got any ideas?
I have tried changing the generated value strategy to IDENTITY but no joy.
New contributor
winarama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.