#Save JSONB to Postgres Spring Boot 3+
1 messages ยท Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
The error you are facing is likely due to an incorrect mapping or data type mismatch between your Java object and the JSONB field in the Postgres database.
To save JSONB data in Postgres using Spring Boot, you can follow these steps:
- Create a model class representing your data:
@Entity
@Table(name = "your_table")
public class YourEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(columnDefinition = "jsonb")
private String jsonData;
// getters and setters
}
-
Use the
@Columnannotation withcolumnDefinition = "jsonb"to specify that the field should be stored as JSONB in the database. -
In your repository interface, extend the
JpaRepositoryinterface:
@Repository
public interface YourRepository extends JpaRepository<YourEntity, Long> {
}
- In your service or controller class, autowire the repository and use it to save the JSONB data:
@Service
public class YourService {
@Autowired
private YourRepository yourRepository;
public void saveJsonData(String jsonData) {
YourEntity entity = new YourEntity();
entity.setJsonData(jsonData);
yourRepository.save(entity);
}
}
- Finally, call the
saveJsonDatamethod with your JSON data to save it in the database:
yourService.saveJsonData("{\"name\": \"John\", \"age\": 30}");
Make sure you have included all necessary dependencies for Spring Data JPA and Postgres in your project's pom.xml or build.gradle file.
If you are still facing any issues or errors, please provide more details about the specific error message you are encountering.
My code:
Entity with JSONB filed
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "subscriptions")
public class Subscription {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(columnDefinition = "jsonb")
private RequestSearchDTO parameters;
}
RequestSearchDTO class
@Builder
@Data
public class RequestSearchDTO implements Serializable {
private Long transportTypeId;
private List<Long> brandId;
private List<Long> modelId;
private List<Long> regionId;
private List<Long> cityId;
}
Controller with RequestSearchDTO
@PostMapping()
public ResponseEntity<String> addSubscription(@ModelAttribute RequestSearchDTO requestSearchDTO) {
Users authenticatedUser = userService.getAuthenticatedUser();
subscriptionService.addSubscription(requestSearchDTO, authenticatedUser);
return ResponseEntity.ok("Subscription added.");
}
And Service
@AllArgsConstructor
@Service
public class SubscriptionService {
private final SubscriptionDAO subscriptionDAO;
private final UserSubscriptionService userSubscriptionService;
@Transactional
public void addSubscription(RequestSearchDTO requestSearchDTO, Users user) {
Subscription subscription = buildSubscriptionByRequestSearchDTO(requestSearchDTO);
save(subscription); // I CATCH EXCEPTION HERE
userSubscriptionService.save(user, subscription);
}
public void save(Subscription subscription) {
subscriptionDAO.save(subscription);
}
private Subscription buildSubscriptionByRequestSearchDTO(RequestSearchDTO requestSearchDTO) {
return Subscription.builder()
.parameters(requestSearchDTO)
.build();
}
}