{"id":313,"date":"2009-12-15T11:09:57","date_gmt":"2009-12-15T11:09:57","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=313"},"modified":"2010-02-21T20:52:10","modified_gmt":"2010-02-21T20:52:10","slug":"accessing-jpa-entity-metadata","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=313","title":{"rendered":"Accessing JPA Entity Metadata"},"content":{"rendered":"<p>It is often useful to access the entity definition metadata. A classic example would be to access the actual maximum defined length of a string column from the code. Such a definition should be centralised to avoid multiple definitions of a constant value, which would otherwise\u00a0require multiple changes if the value were increased. There are several approaches to this, but unfortunately not an obvious &#8216;silver bullet&#8217; which is straightforward and also performant.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">1\/ Access Database Metadata via JDBC<\/span><\/strong><\/p>\n<p>You can access the actual database definitions via a JDBC connection. This would have a performance penalty, but would make sense if the database is your master definition source, i.e. you are starting with a database defninition and then creating entities based on the database.\u00a0You can get a JDBC connection from JPA via the code below,\u00a0as described at the end of <strong><a title=\"http:\/\/wiki.eclipse.org\/EclipseLink\/Examples\/JPA\/EMAPI\" href=\"http:\/\/wiki.eclipse.org\/EclipseLink\/Examples\/JPA\/EMAPI\">this post here<\/a>\u00a0:-<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">JPA 2.0<\/span><\/strong><\/p>\n<pre>entityManager.getTransaction().begin();\r\njava.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);\r\n...\r\nentityManager.getTransaction().commit();<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">JPA 1.0<\/span><\/strong><\/p>\n<pre>entityManager.getTransaction().begin();\r\nUnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)((JpaEntityManager)entityManager.getDelegate()).getActiveSession();\r\nunitOfWork.beginEarlyTransaction();\r\njava.sql.Connection connection = unitOfWork.getAccessor().getConnection();\r\n...\r\nentityManager.getTransaction().commit();<\/pre>\n<p>\u00a0<\/p>\n<p>\u00a0\u00a0<a title=\"http:\/\/www.java2s.com\/Code\/Java\/Database-SQL-JDBC\/GetColumnSize.htm\" href=\"http:\/\/www.java2s.com\/Code\/Java\/Database-SQL-JDBC\/GetColumnSize.htm\"><strong>This post here<\/strong> <\/a>\u00a0details how to get metadata from a connection. The sample code from the post follows :-<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Get Column Size<\/span><\/strong><\/p>\n<pre><span style=\"color: #7f0055;\"><strong>import\u00a0<\/strong><\/span><span style=\"color: #000000;\">java.sql.Connection;<\/span>\r\n<span style=\"color: #7f0055;\"><strong>import\u00a0<\/strong><\/span><span style=\"color: #000000;\">java.sql.DatabaseMetaData;<\/span>\r\n<span style=\"color: #7f0055;\"><strong>import\u00a0<\/strong><\/span><span style=\"color: #000000;\">java.sql.DriverManager;<\/span>\r\n<span style=\"color: #7f0055;\"><strong>import\u00a0<\/strong><\/span><span style=\"color: #000000;\">java.sql.ResultSet;<\/span>\r\n<span style=\"color: #7f0055;\"><strong>import\u00a0<\/strong><\/span><span style=\"color: #000000;\">java.sql.Statement;<\/span>\r\n\r\n<span style=\"color: #7f0055;\"><strong>public\u00a0class\u00a0<\/strong><\/span><span style=\"color: #000000;\">Main\u00a0<\/span><span style=\"color: #000000;\">{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>public\u00a0static\u00a0<\/strong><\/span><span style=\"color: #7f0055;\"><strong>void\u00a0<\/strong><\/span><span style=\"color: #000000;\">main<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">String<\/span><span style=\"color: #000000;\">[]\u00a0<\/span><span style=\"color: #000000;\">args<\/span><span style=\"color: #000000;\">)\u00a0<\/span><span style=\"color: #7f0055;\"><strong>throws\u00a0<\/strong><\/span><span style=\"color: #000000;\">Exception\u00a0<\/span><span style=\"color: #000000;\">{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">Connection\u00a0conn\u00a0=\u00a0getMySqlConnection<\/span><span style=\"color: #000000;\">()<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"Got\u00a0Connection.\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">Statement\u00a0st\u00a0=\u00a0conn.createStatement<\/span><span style=\"color: #000000;\">()<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">st.executeUpdate<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"drop\u00a0table\u00a0survey;\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">st.executeUpdate<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"create\u00a0table\u00a0survey\u00a0(id\u00a0int,name\u00a0varchar(30));\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">st.executeUpdate<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"insert\u00a0into\u00a0survey\u00a0(id,name\u00a0)\u00a0values\u00a0(1,'nameValue')\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">ResultSet\u00a0rsColumns\u00a0=\u00a0<\/span><span style=\"color: #7f0055;\"><strong>null<\/strong><\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">DatabaseMetaData\u00a0meta\u00a0=\u00a0conn.getMetaData<\/span><span style=\"color: #000000;\">()<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">rsColumns\u00a0=\u00a0meta.getColumns<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">null,\u00a0null,\u00a0<\/span><span style=\"color: #2a00ff;\">\"survey\"<\/span><span style=\"color: #000000;\">,\u00a0<\/span><span style=\"color: #7f0055;\"><strong>null<\/strong><\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>while\u00a0<\/strong><\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">rsColumns.next<\/span><span style=\"color: #000000;\">())\u00a0{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0columnName\u00a0=\u00a0rsColumns.getString<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"COLUMN_NAME\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"column\u00a0name=\"\u00a0<\/span><span style=\"color: #000000;\">+\u00a0columnName<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0columnType\u00a0=\u00a0rsColumns.getString<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"TYPE_NAME\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"type:\"\u00a0<\/span><span style=\"color: #000000;\">+\u00a0columnType<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>int\u00a0<\/strong><\/span><span style=\"color: #000000;\">size\u00a0=\u00a0rsColumns.getInt<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"COLUMN_SIZE\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"size:\"\u00a0<\/span><span style=\"color: #000000;\">+\u00a0size<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>int\u00a0<\/strong><\/span><span style=\"color: #000000;\">nullable\u00a0=\u00a0rsColumns.getInt<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"NULLABLE\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>if\u00a0<\/strong><\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">nullable\u00a0==\u00a0DatabaseMetaData.columnNullable<\/span><span style=\"color: #000000;\">)\u00a0{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"nullable\u00a0true\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">}\u00a0<\/span><span style=\"color: #7f0055;\"><strong>else\u00a0<\/strong><\/span><span style=\"color: #000000;\">{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"nullable\u00a0false\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">}<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>int\u00a0<\/strong><\/span><span style=\"color: #000000;\">position\u00a0=\u00a0rsColumns.getInt<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"ORDINAL_POSITION\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"position:\"\u00a0<\/span><span style=\"color: #000000;\">+\u00a0position<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">st.close<\/span><span style=\"color: #000000;\">()<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">conn.close<\/span><span style=\"color: #000000;\">()<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>private\u00a0static\u00a0<\/strong><\/span><span style=\"color: #000000;\">Connection\u00a0getHSQLConnection<\/span><span style=\"color: #000000;\">()\u00a0<\/span><span style=\"color: #7f0055;\"><strong>throws\u00a0<\/strong><\/span><span style=\"color: #000000;\">Exception\u00a0<\/span><span style=\"color: #000000;\">{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>Class<\/strong><\/span><span style=\"color: #000000;\">.forName<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"org.hsqldb.jdbcDriver\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">System.out.println<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #2a00ff;\">\"Driver\u00a0Loaded.\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0url\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"jdbc:hsqldb:data\/tutorial\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>return\u00a0<\/strong><\/span><span style=\"color: #000000;\">DriverManager.getConnection<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">url,\u00a0<\/span><span style=\"color: #2a00ff;\">\"sa\"<\/span><span style=\"color: #000000;\">,\u00a0<\/span><span style=\"color: #2a00ff;\">\"\"<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>public\u00a0static\u00a0<\/strong><\/span><span style=\"color: #000000;\">Connection\u00a0getMySqlConnection<\/span><span style=\"color: #000000;\">()\u00a0<\/span><span style=\"color: #7f0055;\"><strong>throws\u00a0<\/strong><\/span><span style=\"color: #000000;\">Exception\u00a0<\/span><span style=\"color: #000000;\">{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0driver\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"org.gjt.mm.mysql.Driver\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0url\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"jdbc:mysql:\/\/localhost\/demo2s\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0username\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"oost\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0password\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"oost\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>Class<\/strong><\/span><span style=\"color: #000000;\">.forName<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">driver<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">Connection\u00a0conn\u00a0=\u00a0DriverManager.getConnection<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">url,\u00a0username,\u00a0password<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>return\u00a0<\/strong><\/span><span style=\"color: #000000;\">conn;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #000000;\">}<\/span>\r\n\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>public\u00a0static\u00a0<\/strong><\/span><span style=\"color: #000000;\">Connection\u00a0getOracleConnection<\/span><span style=\"color: #000000;\">()\u00a0<\/span><span style=\"color: #7f0055;\"><strong>throws\u00a0<\/strong><\/span><span style=\"color: #000000;\">Exception\u00a0<\/span><span style=\"color: #000000;\">{<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0driver\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"oracle.jdbc.driver.OracleDriver\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0url\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"jdbc:oracle:thin:@localhost:1521:caspian\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0username\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"mp\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">String\u00a0password\u00a0=\u00a0<\/span><span style=\"color: #2a00ff;\">\"mp2\"<\/span><span style=\"color: #000000;\">;<\/span>\r\n\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>Class<\/strong><\/span><span style=\"color: #000000;\">.forName<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">driver<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;\u00a0<\/span><span style=\"color: #3f7f5f;\">\/\/\u00a0load\u00a0Oracle\u00a0driver<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #000000;\">Connection\u00a0conn\u00a0=\u00a0DriverManager.getConnection<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">url,\u00a0username,\u00a0password<\/span><span style=\"color: #000000;\">)<\/span><span style=\"color: #000000;\">;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #7f0055;\"><strong>return\u00a0<\/strong><\/span><span style=\"color: #000000;\">conn;<\/span>\r\n<span style=\"color: #ffffff;\">\u00a0\u00a0<\/span><span style=\"color: #000000;\">}<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">2\/ Access JPA Annotations via Reflection<\/span><\/strong><\/p>\n<p>Alternatively, you could try using Reflection to read the JPA annotations via introspection.\u00a0 <a title=\"http:\/\/www.informit.com\/guides\/content.aspx?g=java&amp;seqNum=372\" href=\"http:\/\/www.informit.com\/guides\/content.aspx?g=java&amp;seqNum=372\"><strong>This post here<\/strong> <\/a>describes how to read annotations with Reflection, but note that this is not a JPA example &#8211; it is a &#8216;roll your own persistance provider&#8217; example, but the introspection techniques should work the same way. The downside of this technique is that all that introspection\u00a0just seems laborious and slow performing if all you want is the length of a few string columns.<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">3\/ Use Constants in Inner Classes to roll your own master Metadata Definitions<\/span><\/strong><\/p>\n<p>Another way, which makes sense if the entity defninitions are the &#8216;master&#8217; and you are creating the database tables from the entities, is just to define some inner classes in the entity class containing static constants for the metadata you need access to. You can then use these definitions as the master, and refer to them both in the annotations and the calling code. You can also use an inheritance hierachy to standardise the metadata you are using, and\u00a0centralise common definitions. As you are rolling your own metadata, you do not have to stick to the jpa annotations, you are free to invent any other constants that you need to tell you something about your entities\/columns. The example which follows demonstrates column metadata, but you could of course apply the same technique to entity (table) level metadata if required.<\/p>\n<p>In\u00a0this example, a number of entities have Code fields containing a coded string which might be 30 characters, and description fields which might be 255 characters. The definitions for &#8220;Code&#8221; and &#8220;Description&#8221; can be centralised in a common superclass. In each Entity, for all the fields for which you want to expose the metadata, you define a public \u00a0inner class named &lt;FieldName&gt;Meta just above the field and annotation declarations. You can then access the constants in the inner class statically from outside the entity. (If required, you could also provide a statically initialised field containing a reference to the &lt;FieldName&gt;Meta class, and create a getter for it, if you need getter access as well, e.g. for\u00a0managed access via\u00a0a framework such as JSF, although this involves non static access to a static value which eclipse\/java will winge about). The following code samples illustrate the technique:-<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Class ColumnMeta<\/span><\/strong><\/p>\n<pre>package uk.co.salientsoft.jpa;\r\n\r\n\/* This base class defines the metadata used. *\/\r\n\r\npublic abstract class ColumnMeta {\r\n\u00a0public static final String NAME = null;\r\n\u00a0public static final int LENGTH = 255;\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Class ColumnMetaCode<\/span><\/strong><\/p>\n<pre>package uk.co.salientsoft.test.domain;\r\nimport uk.co.salientsoft.jpa.ColumnMeta;\r\n\r\npublic abstract class ColumnMetaCode extends ColumnMeta {\r\n\u00a0public static final String NAME = \"Code\";\r\n\u00a0public static final int LENGTH = 30;\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Class ColumnMetaDescription<\/span><\/strong><\/p>\n<pre>package uk.co.salientsoft.test.domain;\r\nimport uk.co.salientsoft.jpa.ColumnMeta;\r\n\r\npublic abstract class ColumnMetaDescription extends ColumnMeta {\r\n\u00a0public static final String NAME = \"Description\";\r\n\u00a0public static final int LENGTH = 255;\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Entity Class AppRole<\/span><\/strong><\/p>\n<pre>package uk.co.salientsoft.test.domain;\r\n\r\nimport java.io.Serializable;\r\nimport java.lang.String;\r\nimport javax.persistence.*;\r\n\r\n@Entity\r\npublic class AppRole implements Serializable {\r\n\r\n\u00a0private static final long serialVersionUID = 1L;\r\n\r\n\u00a0@Id\r\n\u00a0@GeneratedValue(generator=\"AppRoleID\")\r\n\u00a0private long AppRoleID;\r\n\u00a0private long AppID;\r\n\r\n\u00a0public class CodeMeta extends ColumnMetaCode {};\r\n\u00a0@Column(length=CodeMeta.LENGTH)\r\n\u00a0private String Code;\r\n\u00a0\r\n\u00a0public class DescriptionMeta extends ColumnMetaDescription {};\r\n\u00a0@Column(length=DescriptionMeta.LENGTH)\r\n\u00a0private String Description;\r\n\r\n\u00a0public AppRole() {\r\n\u00a0\u00a0super();\r\n\u00a0}\u00a0\u00a0\r\n\u00a0public long getAppRoleID() {\r\n\u00a0\u00a0return this.AppRoleID;\r\n\u00a0}\r\n\u00a0public void setAppRoleID(long AppRoleID) {\r\n\u00a0\u00a0this.AppRoleID = AppRoleID;\r\n\u00a0}\u00a0\u00a0\r\n\u00a0public long getAppID() {\r\n\u00a0\u00a0return this.AppID;\r\n\u00a0}\r\n\u00a0public void setAppID(long AppID) {\r\n\u00a0\u00a0this.AppID = AppID;\r\n\u00a0}\u00a0\u00a0\r\n\u00a0public String getCode() {\r\n\u00a0\u00a0return this.Code;\r\n\u00a0}\r\n\u00a0public void setCode(String Code) {\r\n\u00a0\u00a0this.Code = Code;\r\n\u00a0}\u00a0\u00a0\r\n\u00a0public String getDescription() {\r\n\u00a0\u00a0return this.Description;\r\n\u00a0}\r\n\u00a0public void setDescription(String Description) {\r\n\u00a0\u00a0this.Description = Description;\r\n\u00a0}\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Example MetaData References<\/span><\/strong><\/p>\n<pre>codeLength = AppRole.CodeMeta.LENGTH;\r\ndescLength = AppRole.DescriptionMeta.LENGTH;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It is often useful to access the entity definition metadata. A classic example would be to access the actual maximum defined length of a string column from the code. Such a definition should be centralised to avoid multiple definitions of a constant value, which would otherwise\u00a0require multiple changes if the value were increased. There are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[35,182,16],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/313"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313"}],"version-history":[{"count":22,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":318,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/313\/revisions\/318"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}