1

I am working on a Spring-MVC project in which I am working on creating a backup of GroupNote object every night. Before creating a backup, I am checking if boolean flag indicating that the object was modified is true. If yes, then only a backup is created, and date is already set when the object was created. Now, when a duplicate GroupNote object is created, it has the latest date set.

This way, the user can select a particular date and then retrieve the data-set for that day.

Now, the problem is :

  1. If the object was not modified on Day 3,4,5, but directly on 6th, and then on 7th, once that is done, what if the User tries to retrieve the object from Day-3, it was not modified, but it was on 6th, so how I can implement this logic, where I have to retrieve the next best one.
  2. The other problem I am facing is, GroupNotes have many-to-one mapping with GroupSection. How can I retrieve all GroupNote objects for a given GroupSection for a given date, which wont give all entities which are after Day-3, but only the 6th one.

Here is how every-night I am creating duplicates :

  @Override
    public void  retrieveModifiedNotes() {
        List<GroupNotes> groupNotesList = this.groupNotesDAO.listModifiedNotesForYesterday();
        for(GroupNotes yesterdayNotes : groupNotesList){
            yesterdayNotes.setLatestNote(false);
            this.groupNotesDAO.editGroupNote(yesterdayNotes,yesterdayNotes.getOwnedSectionId());
            GroupNotes newDayNotes = new GroupNotes();
            BeanUtils.copyProperties(yesterdayNotes, newDayNotes);
            newDayNotes.setLatestNote(true);
            newDayNotes.setMnoticesid(0);
            newDayNotes.setGroupNoteHistorySet(yesterdayNotes.getGroupNoteHistorySet());
            newDayNotes.setNoteActivitySet(yesterdayNotes.getNoteActivitySet());
            newDayNotes.setNoteSelectionSet(yesterdayNotes.getNoteSelectionSet());
            newDayNotes.setUnreadNotesSet(null);
            newDayNotes.setPrimaryNote(yesterdayNotes);
            this.groupNotesDAO.addGroupNote(newDayNotes,yesterdayNotes.getOwnedSectionId());
        }
    }

The DAO me to get modified notes :

@Override
    public List<GroupNotes> listModifiedNotesForYesterday() {
        Session session = this.sessionFactory.getCurrentSession();
        Calendar cal = Calendar.getInstance(); // Todays date
        Query query = session.createQuery("from GroupNotes as gs where gs.noteModified=true and gs.latestNote=true and " +
                "gs.noteSavedDate<:loadDate");
        query.setParameter("loadDate", cal.gethodtTime());
        return query.list();
    }

Model GroupNotes :

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


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

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

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


    @ManyToOne
    @JoinColumn(name = "msectionid")
    @JsonIgnore
    private GroupSection ownednotes;
}

Problematic DAO method :

  @Override
    @Transactional(readOnly = true)
    public List<GroupNotes> listGroupNotesBySectionAndDate(int sectionId, Date dateToLoad) {
        Session session = this.sessionFactory.getCurrentSession();
        org.hibernate.Query query = session.createQuery("From GroupNotes as n where n.ownednotes.msectionid=:msectionid and ((n.noteDisabled=false and n.noteInActive=false "
                + "and n.privateNoteUser is null and n.noteSavedDate>:dateToLoad)) order by n.noteSavedDate asc");
        query.setParameter("msectionid", sectionId);
        query.setParameter("dateToLoad", dateToLoad);
        return query.list();
    }

As you can see in the above method, I have no way of limiting the query to only the object found on a specific date, I have a comparator operator, which will give data for any date after/before the one specified.

I hope the problem is clear, if there are any doubts, kindly let me know. Thank you. :-)

We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

0

As far as I understand your problem, your dao method is perfectly fine, but you want to get only the best result (with closest date) not whole list, right?

In that case you should jpa hibernate's paging mechanism to limit the query result to only one row.

This question should be a help.

Community
  • 1
  • 1
Tuan Pham
  • 1,171
  • 2
  • 15
  • 27
  • I don't think so that will help as I can limit the row count with Hibernate as well... When I am retrieving data from GroupSection, I am getting all associated Note objects which fit the condition, not just one. – We are Borg Mar 14 '16 at 09:03