views:

37

answers:

0

We started with a fairly standard relationship between a Project entity and a ProjectStatus entity via a join table. The class declarations (with annotations) look like this.

@Entity
@Table(name="PROJECT")
public class Project {

    @Id
    @Column(name = "PROJECT_ID")
    private Long id;

    @ManyToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinTable(
    name = "PROJECT_STATUS_TRACKING,
        joinColumns = { @JoinColumn(name = "PROJECT_ID") },
        inverseJoinColumns = { @JoinColumn(name = "PROJECT_STATUS_CODE") }
    )
    private ProjectStatus projectStatus;

....
}


@Entity
@Table(name="PROJECT_STATUS")
public class ProjectStatus {

    @Id
    @Column(name = "PROJECT_STATUS_CODE")
    private String code;

    @Column(name="PROJECT_STATUS_DESC")
    private String description
....
}

So far so good. Unfortunately, we now have a new requirement to keep a record of when the project status changes, modeled as a date field in the join table (PROJECT_STATUS_TRACKING).

The obvious solution is to make the join table into an entity. The problem with this is that finding the "current" status now becomes complicated. I'm also not sure how (or if) I can manage to cascade changes into the join table.

The perfect solution would detect a change to projectStatus when I save the Project entity and add a new row to the PROJECT_STATUS_TRACKING table with the correct date and foreign-key values.

Any suggestions? I'm pretty new to Hibernate, so it is entirely possible I'm missing something obvious. Database is Oracle 10. We don't have control over the database schema, but the team that does is willing to listen to good suggestions.

related questions