延迟加载如何工作

时间:2016-08-02 07:28:20

标签: java jpa

我有2个类.Video和VideoLinks.VideoLinks与Video by ManyToOne和LazyLoading.Lazy加载相关意味着当我需要视频时,将获取视频。 我选择了视频链接,然后睡了10秒。我在数据库中更改视频标题,10秒后应用程序打印相关的视频标题。但是这个标题不是标题,而是标题旧标题。

视频:

@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

    }

主:

       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());
         }

0 个答案:

没有答案