In general, I decided to master the JPA in annotations and so far it does not work out very well.

I have a class called GenericAd and Image. Image refers to GenericAd @ManyToOne, and in response to Image there is a List @OneToMany. With such a chip, I want to save a new Image from GenericAd to the database, that is, I put a List in GenericAd and when I save / modify it, the new Image is automatically added to the database. This could be done with xml. But here with JPA I suffered, it does not work.

Here is the mapping:

GenericAd:

@Entity @Table public class GenericAd { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name= "increment", strategy= "increment") @Column(length = 6, nullable = false) private long id; @Column(nullable = false) @Type(type="text") private String description; @Column(nullable = false) private String title; @Column(nullable = false) private String dateAdd; @Column private AdType adType; @Column(nullable = false) private String phoneNumberAuthor; @Column(nullable = false) private String city; @Column private String region; @Column private String district; @Column private String price; @Column(nullable = false) private String adLink; @OneToMany(fetch = FetchType.EAGER, mappedBy = "adEntity", cascade = CascadeType.PERSIST) private List<Image> images; 

Image:

 @Entity @Table public class Image { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name= "increment", strategy= "increment") @Column(length = 6, nullable = false) private long id; @Column(nullable = false) private byte[] image; @ManyToOne() @JoinColumn(name = "ad_id") private GenericAd adEntity; 

Save data via GenericAdDAO using HibernateTemplate enter image description here

At the output, I expect the saved GenericAd (genericad) and Image (in the image table) GenericAd is stored normally, but the Image is not, although with a debugger I checked the List <Image> in GenericAd is not empty before saving.

Here is the database: enter image description here And the image is empty! enter image description here

What could be the problem ?

  • Specify what is stored in the database when saving GenericAd and how it should be? And if there is an opportunity, add the code with which you save GenericAd. - MrFylypenko
  • Thank you for your attention, updated the question. - Andrey Sibirkin
  • Before saving, try to assign a link in Image to GenericAd like this (for (int i = 0; i <genericAd.getImages (). Size (); i ++) {genericAd.getImages (). SetAdEntity (genericAd);}, and The question is, is Image stored separately, without GenericAd? - MrFylypenko
  • I looked at the pictures, you use the save () method, but you need to use persist (), try with this method - MrFylypenko
  • this is genius! With the persist () method everything is preserved! If you want to put in the answers, put a tick) Thank you! - Andrey Sibirkin

1 answer 1

For proper operation of cascade = CascadeType.PERSIST with HibernateTemplate, you need to use the persist () method, not save ()