Note that there are some explanatory texts on larger screens.

plurals
  1. POseparating JPA entities from Hibernate-specific tweaks
    text
    copied!<p>I have the following "root" entity (hibernate-specific parts commented out):</p> <pre><code>@Entity //@GenericGenerator(name="system-uuid",strategy="org.hibernate.id.UUIDGenerator") public class Node extends PersistentEntity { private UUID id; private String name; private String displayName; @Id @GeneratedValue //@GeneratedValue(generator="system-uuid") //instead of above line //@Type(type = "pg-uuid") public UUID getId() { return id; } public String getName() { return name; } public String getDisplayName() { return displayName; } //stuff omitted } </code></pre> <p>this is part of a persistence context deployed on JBoss AS 6 (hibernate 3.6) using PostgreSQL 9 for the database (using the latest JDBC4 driver). PostgreSQL has its own uuid column type, which requires some hibernate-specific mappings to use properly (commented out in the above code) - otherwise hibernate will try to map the UUID field to BINARY, then the PostgreSQL dialect does not support BINARY (apparently because postgre has 2 ways of storing binary and hibernate devs dont like it) and the whole thing explodes.</p> <p>uncommenting the lines above produces working code, but it forces me to have a compile-time dependency on hibernate - which i'd rather avoid.</p> <p>attempting to add an hbm.xml file into the mix and referencing it from the persistence.xml does not merge data from the file and annotations, but simply ignores the annotations:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?> &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> &lt;hibernate-mapping package="package"> &lt;class name="Node"> &lt;id name="id" type="pg-uuid"> &lt;generator class="org.hibernate.id.UUIDGenerator"/> &lt;/id> &lt;/class> &lt;/hibernate-mapping> </code></pre> <p>i can "fluff" this file up by adding the extra 2 properties, but even if i do that (at which point the Node class works), adding any extra entities, like:</p> <pre><code>@Entity public class Host extends Node { //fields, getters, fluff }</code></pre> <p>i get the following exception:</p> <pre><code>org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Node</code></pre> <p>since hibernate "finds" Node twice. is there any elegant way around this? ideally, Node is the only class for which i'll need hibernate-specific properties, and it will be the root of a large hierarchy of classes. i'd like to avoid either compile-time dependencies on hibernate or mapping everything completely in hbm.xml. for completeness' sake, here's the persistence.xml file im using:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?> &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> &lt;persistence-unit name="myPU" transaction-type="JTA"> &lt;jta-data-source>java:/my-postgresql-DS&lt;/jta-data-source> &lt;mapping-file>META-INF/hbm.xml&lt;/mapping-file> &lt;!-- shouldnt need to list classes here since this is deployed --> &lt;!-- in the same jar as the classes and scanning should work --> &lt;validation-mode>AUTO&lt;/validation-mode> &lt;properties> &lt;property name="hibernate.hbm2ddl.auto" value="create-drop"/> &lt;/properties> &lt;/persistence-unit> &lt;/persistence> </code></pre>
 

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