Hello Hibernate community,
I have a Hibernate32 question that has exceeded my meager abilities. I have a join table that I use to map Users to Channels in a many-to-many relationship as per the code below. Has worked pretty well for us so far, the join table has just two columns, user_id and channel_id, and hibernate manages that relationship for me whenever we add or remove Channels to the channels collection.
However, now I have a new business requirement that requires that I keep track of WHEN that relationship was established. The most direct way to do this would be to add a created_time column to subscriber_channel_rt and somehow convince Hibernate to manage this column when it adds or removes channels to a user.
However, I'm unclear about how to convince Hibernate to do such a thing, or even if its possible at all. Does anyone have any suggestions about good ways to proceed? So far the best solution I can think of is to bypass hibernate entirely and use MySQL triggers to manage the created_time column, and direct SQL to query on that time, but I'm hoping there might be a more Hibernate-like way...
Thanks so much,
Mike
PS. bonus points for avoiding ugly migrations
Code:
import javax.persistence.*;
@Entity
public class User
{
// Subscribed channels
protected List<Channel> channels;
@ManyToMany
@JoinTable(name="subscriber_channel_rt",
joinColumns={@JoinColumn(name="user_id")},
inverseJoinColumns={@JoinColumn(name="channel_id")})
@OrderBy(clause="channel1_.name")
public List<Channel> getChannels()
{
return channels;
}
public void setChannels(List<Channel> channels)
{
this.channels = channels;
}
}