I am struggling a bit finding information on the best approach to handle deleting things from a table that comes from an embeddable object.
The parent object will stick around in this case but daily I need to delete the records of the embeddable object that have the prior days date.
Maybe I am doing this the best way but I am not sure how this all works with the Embedded class.
The objects are simplified a bit below.
Code:
@Entity
public class Equity
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false, unique=true)
private String symbol;
private String name;
@CollectionOfElements
@JoinTable(name = "quotes", joinColumns = @JoinColumn(name = "equity_id", nullable = false))
@Sort(type = SortType.NATURAL)
private SortedSet<Quote> quotes = new TreeSet<Quote>();
......
@Embeddable
public class Quote {
@Parent
private Equity equity;
@Column(nullable = false)
private Calendar time;
.......
What I am doing is in a QuoteDao object I have a method that creates a SQL query that deletes everything by date. I had tried to do it in HQL but was having problems maybe due to the @Embeddable not being an Entity.
The only thing I could make work was with straight SQL.
Is this approach below the best way to handle this situation? Any insight on this situation would be great to hear.
Code:
public void deleteOldQuotes(Calendar date) {
try {
Query query = getCurrentSession().createSQLQuery("delete from quotes where time < :purgeBeforeDate");
query.setCalendar("purgeBeforeDate", date);
query.executeUpdate();
}
catch (HibernateException e) {
throw new DaoException(e);
}