Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my opinion hibernate isn't really designed for such scenario. Hibernate requires a pre-defined java class types to map to on compile-time so it knows how to generate SQL.</p> <p>You can however try writing a JDBC ResultSet data access class without hibernate that allows schema changes without requiring java compilation by utilizing ResultSet <a href="http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getMetaData%28%29" rel="nofollow">getMetadata() method</a> and storing all the columns in a Map. The key of the map is the SQL column name:</p> <pre><code>public class Animal { private Map&lt;String,Object&gt; properties = new HashMap&lt;String,Object&gt;(); // getters &amp; setters.. } </code></pre> <p>One problem when using this approach is again you don't know which of the jdbc ResultSet get method you should use: rs.getString(), rs.getInt(), rs.getLong()... ??</p> <p>You can try the generic <a href="http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getObject%28java.lang.String%29" rel="nofollow">rs.getObject(columnName)</a> method. Querying &amp; mapping result set using Spring <a href="http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/jdbc/core/JdbcOperations.html" rel="nofollow">JdbcOperations</a> might look like this:</p> <pre><code>List&lt;Animal&gt; animals = jdbcOperation.query("select * from animal", new RowMapper&lt;Animal&gt;() { @Overrides Animal mapRow(ResultSet rs, int rowNum) throws SQLException { Animal animal = new Animal(); ResultSetMetaData meta = rs.getMetaData(); for(int i = 1; i &lt;= meta.getColumnCount(); i++) { String columnName = meta.getColumnName(i); animal.getProperties().put(columnName, rs.getObject(columnName)); } return animal; } }); </code></pre> <p>Code above will not require re-compilation if you add/remove a column. You can also apply the same concept for updates / insertion. However retrieving the column value gets ugly. Since everything is stored as Object, you need to know what to cast it to.</p> <pre><code>// Getting this animal's name String name = (String) animal.getProperties().get("name"); </code></pre> <p>Other approach I can think of is to use <a href="http://en.wikipedia.org/wiki/NoSQL" rel="nofollow">NoSQL</a> approach with which you don't even need to define a data schema.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload