Angular1+Spring项目中POST完成后执行GET的@ID序列问题排查
Hey there! Let's dig into this ID sequence increment issue you're facing when updating Q&A entries in your Spring+Java+Angular 1 project. I've run into similar scenarios before, so here's what to check and fix:
1. Verify Your JPA Entity's @ID Annotation Setup
The most common culprit here is a misconfigured ID generation strategy in your Java entity. If you're using @GeneratedValue(strategy = GenerationType.SEQUENCE) for your @ID column, make sure JPA isn't treating your update operation as an insert.
- Wrong scenario: If your update logic creates a new entity instance without setting the existing ID (or sets an invalid ID), JPA will try to insert a new record, triggering the sequence increment.
- Fix: Ensure your entity's ID is properly populated before calling the update method, and double-check the generation strategy. For existing records being updated, JPA shouldn't generate a new ID.
Example of correct entity configuration:
@Entity @Table(name = "qa_entries") public class QaEntry { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "qa_seq") @SequenceGenerator(name = "qa_seq", sequenceName = "qa_entry_seq", allocationSize = 1) private Long id; // Other fields, getters, setters... }
2. Check Angular 1's Update Data Payload
In your Angular 1 front-end, when you call updateData after modifying a Q&A entry, make sure you're passing the existing ID along with the updated data. If the ID is missing from the request payload, your backend will likely treat it as a new entry and trigger an insert (and sequence increment).
- Fix: Inspect the request payload in your browser's dev tools (Network tab) to confirm the ID is present. Adjust your Angular code to include the ID when sending the update request.
Example Angular 1 service code:
app.service('QaService', function($http) { this.updateData = function(qaEntry) { // Ensure qaEntry has the 'id' property set return $http.put('/api/qa/update', qaEntry); }; });
3. Fix Backend Update Logic
In your Spring backend, if you're using Spring Data JPA, the save() method behaves differently depending on whether the entity is managed or not:
If the entity has a valid ID that exists in the database,
save()will perform an update.If the entity is a new instance with no ID (or an ID that doesn't exist),
save()will perform an insert.Common mistake: Creating a new entity instance, setting updated fields but forgetting to fetch the existing managed entity first.
Fix: Fetch the existing entity from the repository, update its fields, then save it. Or use
saveAndFlush()on the managed entity.
Example Spring Service code:
@Service public class QaService { @Autowired private QaEntryRepository qaEntryRepository; public QaEntry updateQaEntry(QaEntry updatedEntry) { // Fetch the existing managed entity QaEntry existingEntry = qaEntryRepository.findById(updatedEntry.getId()) .orElseThrow(() -> new RuntimeException("Q&A entry not found")); // Update fields existingEntry.setQuestion(updatedEntry.getQuestion()); existingEntry.setAnswer(updatedEntry.getAnswer()); // Save the managed entity (will trigger update, not insert) return qaEntryRepository.save(existingEntry); } }
4. Rule Out Database-Level Issues
If the above fixes don't work, check your database's sequence configuration and any triggers:
- Ensure the sequence is only configured to increment on INSERT operations, not UPDATE.
- Check if there are any database triggers that accidentally increment the sequence when updating the qa_entries table.
- Enable SQL logging in Spring to see if an INSERT is being called instead of UPDATE during the save operation.
- Inspect the database sequence value before and after an update to confirm it's increasing unexpectedly.
- Verify that the ID passed from Angular to the backend matches the existing record's ID in the database.
内容的提问来源于stack exchange,提问作者Sathish Prakasam

