I am doing a project in Spring and Postgres. I am getting this empty column when I try to call a request with Postman. As you can see, it returns everything except ingredient column.
{
"recept_id": 8,
"recept_name": "conceptual",
"nation_id": 1,
"type_id": 1,
"isvegan": true,
"isvegetarian": true,
"photo": null,
"video": null,
"ingredient": [],
"level_id": 5,
"recept_view": 1,
"company_id": 4,
"ratinglvl": 5
}
However, in Postgres, this column has data ({1,2,3}). The data type of the ingredient column is an integer[] in Postgres. I inserted data to ingredient to Postgres manually.
While in Spring, I am using a simple CRUDrepository.
Entity:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "recept")
public class Recept {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long recept_id;
@Column
private String recept_name;
private long nation_id;
private long type_id;
private boolean isvegan;
private boolean isvegetarian;
private File photo;
private File video;
@ElementCollection(targetClass=Long.class)
private List<Long> ingredient;
private short level_id;
private long recept_view;
private long company_id;
private short ratinglvl;
}
Controller:
@RestController
public class ReceptController {
private final ReceptService receptService;
public ReceptController(ReceptService receptService) {
this.receptService = receptService;
}
@RequestMapping(value="/recept",method= RequestMethod.GET, headers = "Accept=application/json")
public ResponseEntity<?> getAll() {
return ResponseEntity.ok(receptService.getAll());
}
Repository:
public interface ReceptRepository extends CrudRepository<Recept, Long> {}
Service:
@Service
public class ReceptService {
private final ReceptRepository receptRepository;
private final IngredientRepository ingredientRepository;
public ReceptService(ReceptRepository receptRepository, IngredientRepository ingredientRepository) {
this.receptRepository = receptRepository;
this.ingredientRepository = ingredientRepository;
}
public List<Recept> getAll(){
return (List<Recept>)receptRepository.findAll();
}
Don't know why it doesn't return it.
CodePudding user response:
@ElementCollection
is meant to collect the values of a column in a related table -not to denote a PostgreSQL array type.
In order to use Postgresql arrays, you need to define a custom type. Thankfully the hibernate-types library already provides a ListArrayType out of the box. This will allow you to define your entity like:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@TypeDef(
name = "list-array"
typeClass = ListArrayType.class
)
@Table(name = "recept")
public class Recept {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long recept_id;
@Column
private String recept_name;
private long nation_id;
private long type_id;
private boolean isvegan;
private boolean isvegetarian;
private File photo;
private File video;
@ElementCollection(targetClass=Long.class)
@Type(type = "list-array)
@Column(
name = "ingredient",
columnDefinition = "integer[]"
)
private List<Long> ingredient;
private short level_id;
private long recept_view;
private long company_id;
private short ratinglvl;
}