I have 2 classes.Video and VideoLinks.VideoLinks related to Video by ManyToOne and LazyLoading.Lazy loading means when i need Video then Video will be fetched. i select VideoLinks and then sleep 10 seconds.i change Video title in database and after 10 seconds application prints related Video title.But this title is not changed title but old title.
Video:
@Entity
@Table(name = "video")
@XmlRootElement
public class Video implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "title")
private String title;
@Column(name = "url")
private String url;
@Lob
@Column(name = "description")
private String description;
@Column(name = "date")
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@Column(name = "focused_word")
private String focusedWord;
@Column(name = "tags")
private String tags;
@Column(name = "seo_title")
private String seoTitle;
@Column(name = "seo_description")
private String seoDescription;
@Column(name = "category_id")
private String categoryId;
@Column(name = "slug")
private String slug;
@Column(name = "body")
private String body;
@Column(name = "thumb")
private String thumb;
@Column(name = "body_html")
private String bodyHtml;
public Video() {
}
public Video(Integer id) {
this.id = id;
}
//getter-setters
}
VideoLinks:
@Entity
@Table(name = "video_links")
@XmlRootElement
public class VideoLinks implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Column(name = "url")
private String url;
@Column(name = "text")
private String text;
@Basic(optional = false)
@Column(name = "working")
private boolean working;
@JoinColumn(name = "video_id", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Video videoId;
public VideoLinks() {
}
public VideoLinks(Integer id) {
this.id = id;
}
public VideoLinks(Integer id, boolean working) {
this.id = id;
this.working = working;
}
//getter-setters
}
Main:
public static void main(String[] args) {
VideoServiceInter serviceVideo = new VideoService();
VideoLinks videoLinks = serviceVideo.getVideoLinks(15);
System.out.println("i fetched VideoLinks but not Video yet");
try {
Thread.sleep(10000);//at this waiting time i change title in database
} catch (InterruptedException ex) {
LOG.log(Level.SEVERE, null, ex);
}
System.out.println(vl.getVideoId().getTitle());
}