Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate mapping returns null properties
    text
    copied!<p>I have a Hibernate mapping setup. The table is species, my Java class is Species. hibernate.cfg.xml points to mappings in species.hbn.xml</p> <p>In my code I'm using a simple HQL query and then throwing the resultant Species instances into a "SpeciesLister" class (which I'm passing over to the presentation layer).</p> <pre><code> SpeciesLister speciesList = new SpeciesLister(); Query q = session.createQuery("SELECT s FROM Species s"); for (Species s : (List&lt;Species&gt;) q.list()){ speciesList.addSpecies(s); } </code></pre> <p>The Species class looks like this:</p> <pre><code>package springwildlife; public class Species implements Serializable { long id; String commonName; String latinName; String order; String family; ArrayList&lt;Sighting&gt; sightings; public Species() { } public Species(String commonName, String latinName) { sightings = new ArrayList&lt;Sighting&gt;(); this.commonName = commonName; this.latinName = latinName; } public long getId() { return id; } public String getCommonName() { return commonName; } public String getLatinName() { return latinName; } public String getOrder() { return order; } public String getFamily() { return family; } public ArrayList&lt;Sighting&gt; getSightings() { return sightings; } public void addSighting(Sighting s) { sightings.add(s); } public void setId(long id) { this.id = id; } public void setCommonName(String cn) { commonName = cn; } public void setLatinName(String ln) { commonName = ln; } public void setFamily(String f) { family = f; } public void setOrder(String o) { order = o; } } </code></pre> <p>My database schema looks like this:</p> <pre><code>CREATE TABLE species ( id serial NOT NULL, common_name text, latin_name text, order_name text, family_name text, CONSTRAINT id PRIMARY KEY (id) ) </code></pre> <p>species.hbn.xml looks like this:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="springwildlife.Species" table="species"&gt; &lt;id name="id" type="java.lang.Long" column="id" &gt; &lt;generator class="native"&gt; &lt;param name="sequence"&gt;species_id_seq&lt;/param&gt; &lt;/generator&gt; &lt;/id&gt; &lt;property name="commonName" type="java.lang.String"&gt; &lt;column name="common_name" /&gt; &lt;/property&gt; &lt;property name="latinName" type="java.lang.String"&gt; &lt;column name="latin_name"/&gt; &lt;/property&gt; &lt;property name="order" type="java.lang.String"&gt; &lt;column name="order_name"/&gt; &lt;/property&gt; &lt;property name="family" type="java.lang.String"&gt; &lt;column name="family_name"/&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>My SpeciesLister instance gets a full slate of all the expected number of Species instances. However, when I examine the resultant Species instances, all their fields are null except for the id (long), all the others like familyName, latinName, commonName all are null in the mapped object.</p> <p>This is unexpected and I can't figure out why it is happening. Am I doing something wrong? </p> <p>I'm suspicious about two things, but I'm not sure of what to make of them:</p> <ol> <li><p>I think the fact that the id is being property set, but not the other string fields might be a clue.</p></li> <li><p>I suspect something might be wrong with the way I'm casting the objects into a list of Species instances.</p></li> </ol>
 

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