简体   繁体   English

Hibernate 映射返回 null 属性

[英]Hibernate mapping returns null properties

I have a Hibernate mapping setup.我有一个 Hibernate 映射设置。 The table is species, my Java class is Species.表是物种,我的 Java class 是物种。 hibernate.cfg.xml points to mappings in species.hbn.xml hibernate.cfg.xml 指向species.hbn.xml中的映射

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).在我的代码中,我使用了一个简单的 HQL 查询,然后将生成的 Species 实例放入“SpeciesLister”class(我将其传递给表示层)。

   SpeciesLister speciesList = new SpeciesLister();
    Query q = session.createQuery("SELECT s FROM Species s");
    for (Species s : (List<Species>) q.list()){
        speciesList.addSpecies(s);
    }

The Species class looks like this:物种 class 看起来像这样:

package springwildlife;

public class Species implements Serializable
{
    long id;
    String commonName;
    String latinName;
    String order;
    String family;
    ArrayList<Sighting> sightings;

    public Species()
    {
    }

    public Species(String commonName, String latinName)
    {
        sightings = new ArrayList<Sighting>();
        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<Sighting> 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;
    }


}

My database schema looks like this:我的数据库架构如下所示:

CREATE TABLE species
(
  id serial NOT NULL,
  common_name text,
  latin_name text,
  order_name text,
  family_name text,
  CONSTRAINT id PRIMARY KEY (id)
)

species.hbn.xml looks like this: species.hbn.xml 看起来像这样:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="springwildlife.Species" table="species">
      <id name="id" type="java.lang.Long" column="id" >
          <generator class="native">
              <param name="sequence">species_id_seq</param>
          </generator>

  </id>

  <property name="commonName" type="java.lang.String">
   <column name="common_name" />
  </property>
  <property name="latinName" type="java.lang.String">
  <column name="latin_name"/>
  </property>
  <property name="order" type="java.lang.String">
  <column name="order_name"/>
  </property>
  <property name="family" type="java.lang.String">
  <column name="family_name"/>
  </property>
 </class>
</hibernate-mapping>

My SpeciesLister instance gets a full slate of all the expected number of Species instances.我的 SpeciesLister 实例获得了所有预期数量的 Species 实例的完整列表。 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.但是,当我检查生成的 Species 实例时,它们的所有字段都是 null,除了 id (long),所有其他字段,如 familyName、latinName、commonName 都是映射的 ZA8CFDE6331BD59EB66AC96F89111111C4 中的 null。

This is unexpected and I can't figure out why it is happening.这是出乎意料的,我不知道为什么会这样。 Am I doing something wrong?难道我做错了什么?

I'm suspicious about two things, but I'm not sure of what to make of them:我对两件事感到怀疑,但我不确定如何理解它们:

  1. I think the fact that the id is being property set, but not the other string fields might be a clue.我认为 id 正在设置属性,而不是其他字符串字段这一事实可能是一个线索。

  2. I suspect something might be wrong with the way I'm casting the objects into a list of Species instances.我怀疑我将对象转换为 Species 实例列表的方式可能有问题。

The code looks ok.代码看起来没问题。 Without getting into debugger it's hard to tell for sure, however my guess is that you have compile time class instrumentation somewhere in the build.如果不进入调试器,很难确定,但我猜你在构建的某个地方有编译时间 class 工具。 If that's a case, I've seen cases when assignment to actual field in the class is deferred until you call getter method.如果是这种情况,我见过将 class 中的实际字段分配延迟到调用 getter 方法的情况。

So I suggest, that you put some print statements that rely on getters to get data instead of direct access to properties and see what gets printed.因此,我建议您放置一些依赖于 getter 的打印语句来获取数据,而不是直接访问属性并查看打印的内容。

Finally, please put @ sign in front of names in comments (@Mark).最后,请在评论中将@符号放在姓名前(@Mark)。 This way, your correspondents will get notified and you may get response sooner.这样,您的通讯员会收到通知,您可能会更快得到回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM