views:

832

answers:

1

I have a legacy object model that has content objects and a table designed to express relationships between content objects. The latter is called a content_content_connections table, and in addition to having the primary key of the from and to content, it also contains 3 other fields. A connection type field, and content type id fields for both the from and to content. So in the content cbjects a particular kind of connection might be annotated as

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "content_content_connections", joinColumns = { @JoinColumn(name = "CONTENT_ID1") }, inverseJoinColumns = { @JoinColumn(name = "CONTENT_ID2") })
@WhereJoinTable(clause = "connection_type_id=1108")
getFoos() { ... }

Currently this is a read only mapping which ignores the extra columns, aside from the where clause. If I wish to change this to a read/write mapping, is there a way to do so without promoting the join table to a mapped type? In other words, can I use hibernate interceptors to, on save, populate the additional columns in the join table which are not explicitly mapped?

A: 

You are better off implementing a SaveOrUpdateEventListener, rather than an interceptor.

Be sure to add the SaveOrUpdateEventListener this way:

private void initSaveOrUpdateEventListenerHook(Configuration config) {
    List<SaveOrUpdateEventListener> l = new ArrayList<SaveOrUpdateEventListener>();
    SaveOrUpdateEventListener[] listeners =
            config.getEventListeners().getSaveOrUpdateEventListeners();
    l.add(new SaveOrUpdateEventListenerHook());
    l.addAll(Arrays.asList(listeners));
    SaveOrUpdateEventListener[] newListeners = l.toArray(new SaveOrUpdateEventListener[l.size()]);
    config.getEventListeners().setSaveOrUpdateEventListeners(newListeners);
}
Pat