title: How to map a database view without a primary key when using JPA
body: <ul>
  <li>I have views in a SQL database with no obvious primary key (composite or otherwise)</li>
  <li>I would like to access them through JPA</li>
  </ul>
  
  <p>I've read that I should be able to treat views in JPA like I treat tables (using the @Table annotation etc.).  However without a primary key I have to effectively make a composite key out of EVERY COLUMN (in fact, this in what Hibernate's reverse-engineering tool seems to do by default).</p>
  
  <p>However if I do this there are undesirable side effects.
  E.g.</p>
  
  <ul>
  <li><p>Having to write all you code pointing to the primary key's attributes rather than the views:</p>
  
  <p>myViewObject.getPrimaryKey().getFirstName()</p></li>
  <li><p>Not being able to use the "findBy..." methods on the spring Repository (since that attribute is part of the view's "identifier" and not actually one of it's attributes).</p></li>
  </ul>
  
  <p>My question is:  How do I map views in such a was as I can easily access their attributes using JPA?</p>
  
  <p><em>Note:  I'm quite happy to be told I'm using completely the wrong approach.  This seems like such a common problem there's bound to be a better solution.</em></p>