Spring data – Object’s data not filled after saving it

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@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;
}
}
</code>
<code>@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; } } </code>
@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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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());
}
}
</code>
<code>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()); } } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Repository
public interface ArticleRepository extends JpaRepository<ArticleDAO, Integer> {
}
</code>
<code>@Repository public interface ArticleRepository extends JpaRepository<ArticleDAO, Integer> { } </code>
@Repository
public interface ArticleRepository extends JpaRepository<ArticleDAO, Integer> {
}

The suppliers I have in my database:

enter image description here

The call I make to create the article:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[
{
"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
}
}
]
</code>
<code>[ { "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 } } ] </code>
[
        {
            "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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[
{
"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
}
}
]
</code>
<code>[ { "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 } } ] </code>
[
        {
            "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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[
{
"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"
}
}
]
</code>
<code>[ { "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" } } ] </code>
[
        {
            "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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật