I have a simple spring application where I have an ArticleDAO and SupplierDAO. I’m having some problems at retrieving the name of SupplierDAO right after saving ArticleDAO which has a @ManyToOne relationship (many articles can point to the same supplier). When I save it I just have its ID but when I try to retrieve it just after saving it I am not getting its name but if I try to retrieve it afterwards then I get all its info. The SupplierDAO does exist in database and has a name already. Why is this happening?
ArticleDAO and SupplierDAO:
@Getter
@Setter
@NoArgsConstructor
@Entity
@EqualsAndHashCode
@Table(name = "article")
public class ArticleDAO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name", unique = true)
private String name;
@Column(name = "selling_price")
private BigDecimal sellingPrice;
@Column(name = "supplier_price")
private BigDecimal supplierPrice;
@Column(name = "user")
private String user;
@Column(name = "selling_unit_type")
@Enumerated(EnumType.STRING)
private ArticleSellingUnitType sellingUnitType;
@Column(name = "inventory_amount")
private Integer inventoryAmount;
@Column(name = "picture")
private String picture;
@Column(name = "color")
private String color;
@Column(name = "reference")
private String reference;
@Column(name = "bar_code")
private String barCode;
@ManyToOne
@JoinColumn(name = "supplier_id", referencedColumnName = "id")
private SupplierDAO supplier;
@Getter
@Setter
@NoArgsConstructor
@Entity
@EqualsAndHashCode
@Table(name = "supplier")
public static class SupplierDAO {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
}
}
Service method to save and get all info from SupplierDAO:
public class ArticlesServiceImpl implements ArticlesService {
private final ArticleRepository articleRepository;
private final ModelMapper modelMapper;
@Override
public List<Article> createArticles(final List<Article> articles) {
List<ArticleDAO> articleDAOS this.modelMapper.map(articles, new TypeToken<List<ArticleDAO>>(){}.getType());
this.articleRepository.saveAll(articleDAOS);
articleDAOS = this.articleRepository.findAll();
return this.modelMapper.map(articleDAOS, new TypeToken<List<Article>>(){}.getType());
}
}
Article’s Repository:
@Repository
public interface ArticleRepository extends JpaRepository<ArticleDAO, Integer> {
}
The suppliers I have in my database:
The call I make to create the article:
[
{
"name": "Botellín Mahou Clasica 23334333",
"sellingPrice": 1.50,
"supplierPrice": 0.44,
"sellingUnitType": "WEIGHT",
"inventoryAmount": 100,
"picture": "picture.png",
"color": "blue",
"barCode": "123123123123",
"reference": "800",
"supplier": {
"id": 6
}
}
]
The response I get (the error where supplier’s name is missing) right after creating the article:
[
{
"name": "Botellín Mahou Clasica 23334333",
"sellingPrice": 1.50,
"supplierPrice": 0.44,
"sellingUnitType": "WEIGHT",
"inventoryAmount": 100,
"picture": "picture.png",
"color": "blue",
"barCode": "123123123123",
"reference": "800",
"supplier": {
"id": 6,
"name": null
}
}
]
The response I get if I retrieve all the articles through another method after the article has already been created:
[
{
"name": "Botellín Mahou Clasica 23334333",
"sellingPrice": 1.50,
"supplierPrice": 0.44,
"sellingUnitType": "WEIGHT",
"inventoryAmount": 100,
"picture": "picture.png",
"color": "blue",
"barCode": "123123123123",
"reference": "800",
"supplier": {
"id": 6,
"name": "VFGBO"
}
}
]
Why am I not getting the supplier’s name on the first call when I’m creating the article? I am using MySql incase it’s realed somehow.
Thanks!