Currently, I have a class which contains a set of String objects from a child table. All of the examples that I've seen only show the xml version of this setup, and using the hbm.xml file, this is working just fine.
I would like to move entirely to annotations, but haven't found an equivalent way of specifying which database column to use in the annotations (the element/column/type section of set in the xml).
When I attempt to run without the hbm.xml, the logs complain about a missing "column" since the "element" isn't defined.
Is there a way of doing this with Hibernate specific annotations? Or am I stuck with the hbm.xml files for now?
Thanks.
Hibernate version:
Hibernate EntityManager 3.2.1.GA
Hibernate Annotations 3.2.1.GA
App Server version:
JBoss [Zion] 4.0.4.GA (build: CVSTag=JBoss_4_0_4_GA date=200605151000)
Mapping documents:
Inventory.hbm.xml
Code:
<hibernate-mapping>
<class name="some.package.Inventory" table="Inventory">
<id name="myId" type="long" column="inventory_id">
<generator class="sequence"/>
</id>
<!-- other <property elements for other columns, etc... -->
<set name="myApplications" table="inventory_applications" cascade="all">
<key column="inventory_id"/>
<element column="app_name" type="string"/>
</set>
</class>
</hibernate-mapping>
Java File
Code:
@Entity
@Table(name="Inventory")
public class Inventory
implements Serializable
{
private long myId;
private Set<String> myApplications = new HashSet<String>(0);
...snip simple getters/setters/field declarations ...
@CollectionOfElements()
@JoinTable(name="inventory_applications", joinColumns = {
@JoinColumn(name="inventory_id", nullable=false, updatable=false) })
public Set<String> getMyApplications() {
return this.myApplications;
}
public void setMyApplications(Set<String> myApplications) {
this.myApplications = myApplications;
}
}
Simplified Database Schema
Code:
INVENTORY
=========
inventory_id (long - pk)
** other columns **
INVENTORY_APPLICATIONS
========================
inventory_id (long - fk - nonunique)
app_name (string)
Full stack trace of any exception that occurs:
Caused by: org.postgresql.util.PSQLException: ERROR: column myapplicat2_.element does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1512)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1297)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:430)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:346)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:250)
at org.jboss.resource.adapter.jdbc.CachedPreparedStatement.executeQuery(CachedPreparedStatement.java:90)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:236)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
... 42 more
Name and version of the database you are using:
RDBMS: PostgreSQL, version: 8.1.3
JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.1 JDBC3 with SSL (build 404)