2016-03-01 16 views
10

Tôi đang làm việc trên một dự án Spring-MVC, hiện tôi đang làm việc trên chức năng Dòng thời gian. Tôi có một cơ sở hạ tầng cơ bản đã được tiến hành, nhưng hiện tại, tôi đang xử lý ánh xạ và cách tránh tạo bản sao cho chức năng Dòng thời gian.Tránh tạo bản sao cho chức năng TImeline trong Spring

trạng:

Trong công cụ của chúng tôi, có GroupSection trong đó có một one-to-many lập bản đồ với GroupNote. Đối tượng GroupNote có ánh xạ một-nhiều với Đính kèm, Lịch sử.

Chức năng dòng thời gian này là gì?

Trong chức năng dòng thời gian, bất kỳ Người dùng nào cũng có thể nhảy bất kỳ lúc nào và kiểm tra nội dung của GroupSection, GroupNotes, tệp đính kèm và Lịch sử.

Tôi đang lập kế hoạch để triển khai nó như thế nào?

Tôi có 4 biến trong mỗi đối tượng trên để xử lý việc này. Chúng là Date SavedDate, boolean initialNote, boolean noteModified, boolean latestNote.

Cùng với đó, mỗi GroupNote sẽ tự tham gia, sẽ sớm giải thích.

Bây giờ, mỗi lần Ghi chú được sửa đổi, cờ sửa đổi được đặt thành true. Trong đêm, tôi chạy một phương thức để kiểm tra tất cả các đối tượng đã sửa đổi và chỉ khi đối tượng được sửa đổi, nó tạo ra một thể hiện trùng lặp cho Object, đặt Date mới vào nó, đánh dấu nó là mới nhất và vẫn tồn tại .

Bằng cách này, tôi có thể tải tất cả các đối tượng trong một ngày nhất định. Và để sử dụng bình thường hàng ngày, tất cả các ghi chú được đánh dấu là latest, sẽ được tải.

Bất cứ khi nào một đối tượng mới được tạo cho sự bền bỉ, nó sẽ tự nối với đối tượng cũ, với Cascade.Remove. Điều này có nghĩa là, nếu người dùng quay trở lại và loại bỏ đối tượng từ năm 2015, thì tất cả các đối tượng tiếp theo cho tới hiện tại cũng sẽ bị loại bỏ. Điều này cho một thời gian như hành vi.

Vấn đề:

Bây giờ, nếu một GroupSection được sửa đổi, sau đó tôi sẽ tạo ra một thể hiện của GroupSection và duy trì nó, bởi tính từ GroupSection sửa đổi sao chép, và đánh dấu nó như là mới nhất.

Bây giờ, các ghi chú mà GroupSection đang giữ không được sửa đổi, nhưng nếu tôi không tạo các mục trùng lặp, thì tôi sẽ không thấy bất kỳ Ghi chú nào được tải trong giao diện người dùng. Nhưng tôi muốn tránh điều này. Làm thế nào tôi có thể làm điều đó?

Mã cuối cùng: mô hình

GroupSection:

@Entity 
@Table(name = "membersection") 
public class GroupSection { 

@Column(name = "section_save_date", columnDefinition = "date") 
    private Date secnSavedDate; 

    @Column(name = "initial_section", columnDefinition = "boolean default true") 
    private boolean initialSection; 

    @Column(name = "section_modified", columnDefinition = "boolean default false") 
    private boolean sectionModified; 

    @Column(name = "latest_section", columnDefinition = "boolean default false") 
    private boolean latestSection; 


    @JsonIgnore 
    @ManyToOne 
    @JoinColumn(name = "owned_section_id", nullable = true) 
    private GroupSection primarySection; 

    @OneToMany(mappedBy = "primarySection", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) 
    private Set<GroupSection> groupSectionSet = new HashSet<>(); 

    @OneToMany(mappedBy = "ownednotes", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) 
    @JsonIgnore 
    private Set<GroupNotes> sectionsnotes = new HashSet<>(); 
} 

GroupNotes mô hình:

@Entity 
@Table(name = "groupnotes") 
public class GroupNotes implements Serializable { 

    @Column(name = "note_save_date", columnDefinition = "date") 
    private Date noteSavedDate; 

    @Column(name = "initial_note", columnDefinition = "boolean default true") 
    private boolean initialNote; 

    @Column(name = "note_modified", columnDefinition = "boolean default false") 
    private boolean noteModified; 

    @Column(name = "latest_note", columnDefinition = "boolean default false") 
    private boolean latestNote; 

@JsonIgnore 
    @ManyToOne 
    @JoinColumn(name = "owned_note_id", nullable = true) 
    private GroupNotes primaryNote; 

    @OneToMany(mappedBy = "primaryNote", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) 
    private Set<GroupNotes> groupNotesSet = new HashSet<>(); 
} 

GroupSectionDAOImpl:

@Override 
    @Transactional(readOnly = true) 
    public List<GroupSection> listGroupSectionByCanvasAndDate(int mcanvasid, Date dateToLoad) { 
     Session session = this.sessionFactory.getCurrentSession(); 
     org.hibernate.Query query = session.createQuery("From GroupSection as msection where " + 
       "msection.currentcanvas.mcanvasid=:mcanvasid and " + 
       "msection.secnSavedDate>:dateToLoad " + 
       " and msection.sectionDisabled=false and msection.latestSection=true and msection.sectionInActive=false order by msection.secnSavedDate asc"); 
     query.setParameter("mcanvasid", mcanvasid); 
     query.setParameter("dateToLoad",dateToLoad); 
     return query.list(); 
    } 

    @Override 
    public List<GroupSection> listModifiedSectionsForYesterday() { 
     Session session = this.sessionFactory.getCurrentSession(); 
     Calendar cal = Calendar.getInstance(); 
     Query query = session.createQuery("from GroupSection as gs where gs.sectionModified=true and gs.latestSection=true and gs.secnSavedDate<:loadDate order by gs.secnSavedDate asc"); 
     query.setParameter("loadDate",cal.getTime()); 
     return query.list(); 
    } 

Các phương pháp DAO ở trên cung cấp cho tôi các phần được sửa đổi từ ngày hôm qua hoặc phần sửa đổi lần cuối và các phần tương tự cho ngày cụ thể. Tôi có phương pháp tương tự trong GroupNotesDAOImpl

GroupSectionServiceImpl:

@Override 
    public List<GroupSection> retrieveModifiedSections() { 
      List<GroupSection> groupSectionList = this.groupSectionDAO.listModifiedSectionsForYesterday(); 
     for (GroupSection yesterdaySection : groupSectionList) { 
      yesterdaySection.setLatestSection(false); 
      this.groupSectionDAO.updateGroupSection(yesterdaySection); 
      GroupSection newDaySection = new GroupSection(); 
      BeanUtils.copyProperties(yesterdaySection, newDaySection); 
      newDaySection.setInitialSection(false); 
      newDaySection.setSectionModified(false); 
      newDaySection.setLatestSection(true); 
      newDaySection.setMsectionid(0); 
      newDaySection.setSortedSectionSet(null); 
      newDaySection.setSecnSavedDate(Calendar.getInstance().getTime()); 
      int sectionSavedId = directAddGroupSection(newDaySection, yesterdaySection.getCurrentCanvasId()); 

      List<GroupNotes> groupNotesList = this.groupNotesService.directListGroupNotesBySectionIdForCanvasCopying(yesterdaySection.getMsectionid()); 

      for(GroupNotes groupNotes : groupNotesList){ 
    Problem -->   // No option but to create duplicates. 
      } 

     } 
     return this.groupSectionDAO.listModifiedSectionsForYesterday(); 

    } 

Tôi hy vọng câu hỏi là điều dễ hiểu. Nếu có bất kỳ nghi ngờ nào, vui lòng cho tôi biết.

Sửa

Một chiến lược tôi có thể nghĩ đang diễn ra với nhiều-nhiều ánh xạ giữa GroupSection và GroupNotes. Thật không may, rất nhiều phụ trợ cũng như mã front-end đã được phát triển với ánh xạ một-nhiều giữa GroupSection và GroupNotes. Tuy nhiên, tôi đã thêm nhiều ánh xạ nhiều, nhưng tập dữ liệu không được tải, đồng thời nó cũng làm tăng độ phức tạp, một câu hỏi đang tiến hành here. Tôi vẫn thích một số lựa chọn khác.

+0

hm ....những gì về tách thực thể, thiết lập id để null, làm thay đổi cần thiết và bền bỉ như mô tả ở đây? http://stackoverflow.com/questions/11625096/cloning-jpa-entity – ivanenok

+0

@ MaximS.Ivanov: Đó chỉ là tạo ra một bản sao, một vấn đề tôi muốn tránh một cách rõ ràng. –

Trả lời

0

Tôi không chắc mình đã kích hoạt chức năng xóa này như thế nào. Với tôi một chiến lược khả thi sẽ là theo quan hệ một-nhiều từ GroupSection lịch sử (đầu tiên được xóa của chuỗi lịch sử) cho các GroupNotes liên quan. Mối quan hệ này cần phải xác định từng nhóm đối tượng bị ảnh hưởng bị ảnh hưởng tương ứng và xóa một phiên bản như vậy.

Bạn có thể cần thu thập các tham chiếu đến các GroupNotes đó trong khi làm việc lên các GroupSections (có các thay đổi trong tập hợp này theo thời gian, tôi giả định) và dọn dẹp tập kết quả ở cuối để tránh kiểm tra GroupNotes obver và hơn .

0

Tôi đề nghị thay đổi liên kết giữa GroupSection và GroupNotes thành nhiều người để tránh trùng lặp với nhóm.

@Entity 
@Table(name = "membersection") 
public class GroupSection { 

@Column(name = "section_save_date", columnDefinition = "date") 
    private Date secnSavedDate; 

    @Column(name = "initial_section", columnDefinition = "boolean default true") 
    private boolean initialSection; 

    @Column(name = "section_modified", columnDefinition = "boolean default false") 
    private boolean sectionModified; 

    @Column(name = "latest_section", columnDefinition = "boolean default false") 
    private boolean latestSection; 


    @JsonIgnore 
    @ManyToOne 
    @JoinColumn(name = "owned_section_id", nullable = true) 
    private GroupSection primarySection; 

    @OneToMany(mappedBy = "primarySection", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE) 
    private Set<GroupSection> groupSectionSet = new HashSet<>(); 

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    @JoinTable(name = "groupsection_groupnote", joinColumns = { 
      @JoinColumn(name = "GROUP_SECTION_ID", nullable = false, updatable = false) }, 
      inverseJoinColumns = { @JoinColumn(name = "GROUP_NOTE_ID", 
        nullable = false, updatable = false) }) 
    @JsonIgnore 
    private Set<GroupNotes> sectionsnotes = new HashSet<>(); 
} 

GroupSectionServiceImpl:

@Override 
    public List<GroupSection> retrieveModifiedSections() { 
      List<GroupSection> groupSectionList = this.groupSectionDAO.listModifiedSectionsForYesterday(); 
     for (GroupSection yesterdaySection : groupSectionList) { 
      yesterdaySection.setLatestSection(false); 
      this.groupSectionDAO.updateGroupSection(yesterdaySection); 
      GroupSection newDaySection = new GroupSection(); 
      BeanUtils.copyProperties(yesterdaySection, newDaySection); 
      newDaySection.setInitialSection(false); 
      newDaySection.setSectionModified(false); 
      newDaySection.setLatestSection(true); 
      newDaySection.setMsectionid(0); 
      newDaySection.setSortedSectionSet(null); 
      newDaySection.setSecnSavedDate(Calendar.getInstance().getTime()); 
      int sectionSavedId = directAddGroupSection(newDaySection, yesterdaySection.getCurrentCanvasId()); 

      List<GroupNotes> groupNotesList = this.groupNotesService.directListGroupNotesBySectionIdForCanvasCopying(yesterdaySection.getMsectionid()); 

    newDaySection.setGroupNotesSet(groupNotesList); 

     } 
     return this.groupSectionDAO.listModifiedSectionsForYesterday(); 

    } 
0

Làm thế nào về việc có @ManyToOne cho GroupNote -> GroupSection, thay vì đề cập GroupNote trong GroupSection?

Khi cần phải sửa đổi bất kỳ GroupSection nào, bạn chỉ có thể cập nhật GroupSection và ngày sửa đổi của nó. Nếu bạn muốn GroupNote cũng được cập nhật, bạn có thể xử lý nó ở cấp dịch vụ, bằng truy vấn riêng biệt.

Các vấn đề liên quan