title: Map domain model to legacy database
body: <p>First of all I need to say that I'm not an expert in Hibernate. I only used the basics of it in other projects. But now, I want to use Hibernate on a legacy database scheme but I'm experiencing some difficulties to map tables to a Java domain model:</p>
  
  <p>1) Lots of tables have a primary key (as a String) which are assigned by the user. So they are not generated according some algorithm. They in fact are what Hibernate calls 'natural' keys.
  Is there a way to tell Hibernate to accept an ID assigned by the user?</p>
  
  <p>2) The second problem is that lot of tables have some common properties like: valid_from, valid_to, date_created, date_last_modified, user_last_modified &amp; user_created.
  These properties are used for auditing (which record was changed by which user) and to disable records when the 'valid_to' date field is in the past. I don't want to expose these fields to the end user because they seem to be 'meta data', but I have to find a way to get these fields updated. Is there a generic solution in Hibernate that I can use to update these fields whenever a record is changed? For information, these fields are also used in 'link tables' which are used to manage many-to-many relationships between 2 tables.</p>
  
  <p>All help is very appreciated!</p>
  
  <p>update:
  I will use an AbstractEntity to hold the common properties and use an EventListener to insert/update these fields. The user will be retrieved by something UserContext.getCurrentUser() (the user will be bound to the current thread using ThreadLocal).</p>
  
  <p>The only issue now is how to deal with those 'association' tables... They also contain those common fields and I don't want to create a separate Class to map such association table. In my opinion, Hibernate does a great job to hide such link-table. I there a way to update such link table with an EventListener?</p>
  
  <p>For instance: if table A has a many-to-many relation with class B via link table A_B, is there a way to update A_B whenever a record is inserted/updated on A or B?</p>