Hibernate映射返回null属性 [英] Hibernate mapping returns null properties

查看:85
本文介绍了Hibernate映射返回null属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Hibernate映射设置。该表是物种,我的Java类是物种。 hibernate.cfg.xml指向species.hbn.xml中的映射

在我的代码中,我使用一个简单的HQL查询,然后将生成的Species实例投入到 SpeciesLister类(我将它传递给表示层)。

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

Species类如下所示:

  package springwildlife; 

public class Species implements Serializable
{
long id;
字符串commonName;
字符串latinName;
字符串顺序;
字符串系列;
ArrayList< Sighting>目击;

public物种()
{
}

公共物种(字符串commonName,字符串latinName)
{
目击=新的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;
}


}

我的数据库模式看起来像这样:

  CREATE TABLE种类

id序列NOT NULL,
common_name文本,
latin_name文本,
order_name文本,
family_name文本,
CONSTRAINT ID PRIMARY KEY(ID)


$ b species.hbn.xml看起来像这样:

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

< hibernate-mapping>
< class name =springwildlife.Speciestable =species>
< id name =idtype =java.lang.Longcolumn =id>
< generator class =native>
< param name =sequence> species_id_seq< / param>
< / generator>

< / id>

< property name =commonNametype =java.lang.String>
< column name =common_name/>
< / property>
< property name =latinNametype =java.lang.String>
< column name =latin_name/>
< / property>
< property name =ordertype =java.lang.String>
< column name =order_name/>
< / property>
< property name =familytype =java.lang.String>
< column name =family_name/>
< / property>
< / class>
< / hibernate-mapping>

My SpeciesLister实例获取所有预期数量的Species实例的完整版本。但是,当我检查生成的Species实例时,除了id(long)之外,它们的所有字段都为null,所有其他像familyName,latinName,commonName都在映射对象中为null。



这是意想不到的,我无法弄清楚它为什么会发生。难道我做错了什么?



我对两件事感到怀疑,但我不确定要做些什么:


  1. 我认为id是属性设置,而不是其他字符串字段可能是一个线索。


  2. 我怀疑某些东西可能是错误的,因为我将这些对象转换为Species实例列表。



解决方案

代码看起来不错。没有进入调试器很难说清楚,但是我的猜测是你在编译的某个地方有编译时间类的工具。如果是这种情况,我已经看到了在实际字段中赋值给该类的情况,直到你调用getter方法。



所以我建议,你把一些print这些语句依赖getters来获取数据,而不是直接访问属性,并查看打印内容。



最后,请将@标记放在评论名称的前面(@Mark )。这样,你的记者会收到通知,你可能会得到更快的反应。

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

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).

   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:

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:

<?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. 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.

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.

  2. I suspect something might be wrong with the way I'm casting the objects into a list of Species instances.

解决方案

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. If that's a case, I've seen cases when assignment to actual field in the class is deferred until you call getter method.

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.

Finally, please put @ sign in front of names in comments (@Mark). This way, your correspondents will get notified and you may get response sooner.

这篇关于Hibernate映射返回null属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆