Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我能';t使用Hibernate和Criteria以一对多关系访问查询后的列表_Hibernate_List_Criteria_One To Many - Fatal编程技术网

我能';t使用Hibernate和Criteria以一对多关系访问查询后的列表

我能';t使用Hibernate和Criteria以一对多关系访问查询后的列表,hibernate,list,criteria,one-to-many,Hibernate,List,Criteria,One To Many,我对Hibernate、一对多关联和列表有问题。在我的模型中,我有国家,对于每个国家,我有多个信息描述该国家的地址结构。我想访问这些元素的列表 这是我的国家: public class Country { private Long id; private String name; private List<AddressElement> addressElementList = null; ... } 和我的映射: <hibernate-mapping pa

我对Hibernate、一对多关联和列表有问题。在我的模型中,我有国家,对于每个国家,我有多个信息描述该国家的地址结构。我想访问这些元素的列表

这是我的国家:

public class Country {
  private Long id;
  private String name;
  private List<AddressElement> addressElementList = null;
  ...
}
和我的映射:

<hibernate-mapping  package="org.devtutos.tuto5.domaine">
  <class  name="Country" table="tuto.COUNTRY">
    <id name="id" type="java.lang.Long" column="COUNTRY_ID" unsaved-value="null">
      <generator class="native">
        <param name="sequence">tuto.SEQ_COUNTRY</param>
      </generator>
    </id>
    <natural-id mutable="true">
      <property name="name" column="COUNTRY_NAME" type="java.lang.String"/>
    </natural-id>
    <list name="addressElementList" table="tuto.ADDRESS_ELEMENT" lazy="false">
      <key  foreign-key="country_address_element_fk">
        <column name="COUNTRY_ID" not-null="true"/>
      </key>
      <list-index>
        <column name="ADDRESS_ELEMENT_POS" not-null="true" />
      </list-index>
      <one-to-many class="AddressElement"/>
    </list>
  </class>
</hibernate-mapping>
<hibernate-mapping  package="org.devtutos.tuto5.domaine">
  <class name="AddressElement" table="tuto.ADDRESS_ELEMENT">
    <id name="id" type="java.lang.Long" column="ADDRESS_ELEMENT_ID" unsaved-value="null">
      <generator class="native">
        <param name="sequence">tuto.SEQ_ADDRESS_ELEMENT</param>
      </generator>
    </id>
    <property name="line" column="ADDRESS_ELEMENT_LINE" not-null="true" type="java.lang.Integer" />
    <property name="name" column="ADDRESS_ELEMENT_NAME" not-null="true" type="java.lang.String" />
    <property name="mask" column="ADDRESS_ELEMENT_TYPE_MASK" not-null="true" type="java.lang.String" />
    <property name="pos" column="ADDRESS_ELEMENT_POS" not-null="true" type="java.lang.Integer" />
  </class>
</hibernate-mapping>
这是我的日志:

Hibernate: 
select 
this_.COUNTRY_ID as COUNTRY1_2_1_, 
this_.COUNTRY_NAME as COUNTRY2_2_1_, 
addressele2_.COUNTRY_ID as COUNTRY6_2_3_, 
addressele2_.ADDRESS_ELEMENT_ID as ADDRESS1_3_, 
addressele2_.ADDRESS_ELEMENT_POS as ADDRESS5_3_, 
addressele2_.ADDRESS_ELEMENT_ID as ADDRESS1_0_0_, 
addressele2_.ADDRESS_ELEMENT_LINE as ADDRESS2_0_0_, 
addressele2_.ADDRESS_ELEMENT_NAME as ADDRESS3_0_0_, 
addressele2_.ADDRESS_ELEMENT_TYPE_MASK as ADDRESS4_0_0_, 
addressele2_.ADDRESS_ELEMENT_POS as ADDRESS5_0_0_ 
from 
tuto.COUNTRY this_ left outer join tuto.ADDRESS_ELEMENT addressele2_ 
on this_.COUNTRY_ID=addressele2_.COUNTRY_ID 
where 
this_.COUNTRY_NAME=?
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Pays trouvé.
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Nom du Pays :FRANCE
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Code du Pays :1
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Taille de la liste des éléments d'adresse :4
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: L'élément d'adresse est null.
27 déc. 2012 16:19:09 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [tuto5] in context with path [/tuto-5] threw exception [Request processing failed; nested exception is org.springframework.webflow.execution.ActionExecutionException: Exception thrown executing [AnnotatedAction@3f610944 targetAction org.devtutos.tuto5.mvc.webflow.action.receptionAction@4dc6bbd3, attributes = map['method' -> 'enregistrerLUtilisateur']] in state 'enregistrerLUtilisateur' of flow 'reception' -- action execution attributes were 'map[[empty]]'] with root cause
java.lang.NullPointerException
at org.devtutos.tuto5.dao.CountryHibernate.getCountryByName(CountryHibernate.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
...
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
我无法访问来自某个国家/地区的地址元素信息。我尝试了几件事:我打开了惰性模式,并用JOIN指定了一个获取模式,但它不起作用。最令人惊讶的是,该列表已填充(共有4个元素),并且以下代码运行良好(因此,信息在数据库中,setter和getter工作正常):

以下是此代码的结果:

INFO: Lecture de tous les éléments d'adresse.
Hibernate: select this_.ADDRESS_ELEMENT_ID as ADDRESS1_0_0_, this_.ADDRESS_ELEMENT_LINE as ADDRESS2_0_0_, this_.ADDRESS_ELEMENT_NAME as ADDRESS3_0_0_, this_.ADDRESS_ELEMENT_TYPE_MASK as ADDRESS4_0_0_, this_.ADDRESS_ELEMENT_POS as ADDRESS5_0_0_ from tuto.ADDRESS_ELEMENT this_
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Taille de la liste des éléments d'adresse : 6
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : ID = 1
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : POS = 1
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : NOM = Code Postal
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : ID = 2
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : POS = 2
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : NOM = Ville
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : ID = 3
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : POS = 3
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : NOM = Batiment
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : ID = 4
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : POS = 1
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : NOM = District
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : ID = 5
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : POS = 2
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : NOM = Zone
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : ID = 6
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : POS = 1
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
INFO: Element de la liste : NOM = City
27 déc. 2012 16:19:09 org.devtutos.tuto5.dao.CountryHibernate getCountryByName
在第一次执行时,我注意到列表的大小是错误的(4而不是3)。在我的示例中,
countryName
的值是“FRANCE”。这是我表格的内容:

国家:

COUNTRY_ID      CONTRY_NAME 
1                    "FRANCE"
2                    "HYPERION"
3                    "ATLANTIS"
地址单元:

ADDRESS_ELEMENT_ID      COUNTRY_ID       ADDRESS_ELEMENT_POS    ADDRESS_ELEMENT_NAME        ADDRESS_ELEMENT_MASK    ADDRESS_ELEMENT_LINE
1                       1               1                       "Code Postal"               "\d{5}"                 1
2                       1               2                       "Ville"                     "[A-Z\-]{1,50}"         1
3                       1               3                       "Batiment"                  "[A-F]"                 1
4                       2               1                       "District"                  "[A-Z]{1,20}"           1 
5                       2               2                       "Zone"                      "[A-5]{1,5}"            2
6                       3               1                       "City"                      "[A-Z]{1,9}"            1
我做错了什么?我不明白为什么
elem
在第一次执行时为空。有人能帮我吗

提前谢谢

祝你新年快乐

谢谢你的帮助

这是国家实体的完整代码。只有能手和二传手。hashCode()和equals(objectobj)方法已由Eclipse自动生成

package org.devtutos.tuto5.domaine;

import java.util.ArrayList;
import java.util.List;

public class Country {

    private Long id;
    private String name;
    private List<AddressElement> addressElementList = null;

    public Country(){}

    public Long getId() {
        return id;
    }  

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   

    public void addAddressElement( int pos, int line, String name, String mask){
        addressElementList.add(new AddressElement(pos,line,name,mask));
    }

    public List<AddressElement> getAddressElementList() {
        return addressElementList;
    }

    public void setAddressElementList(List<AddressElement> addressElementList) {
        this.addressElementList = addressElementList;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
             return false;
        if (getClass() != obj.getClass())
            return false;
        Country other = (Country) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}
package org.devtutos.tuto5.domaine;
导入java.util.ArrayList;
导入java.util.List;
公营国家{
私人长id;
私有字符串名称;
私有列表addressElementList=null;
公共国家({}
公共长getId(){
返回id;
}  
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}   
public void addAddressElement(整数位置、整数行、字符串名称、字符串掩码){
addressElementList.add(新的AddressElement(位置、行、名称、掩码));
}
公共列表getAddressElementList(){
返回地址元素列表;
}
public void setAddressElementList(列表addressElementList){
this.addressElementList=addressElementList;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((id==null)?0:id.hashCode();
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
国家/地区其他=(国家)obj;
if(id==null){
if(other.id!=null)
返回false;
}如果(!id.equals(other.id))
返回false;
返回true;
}
}
我不知道问题出在哪里。
再次感谢…

您的列表是由列
地址元素\u POS
索引的。因此,如果Hibernate加载国家的所有地址列表
1
,它将在索引1处设置地址
1
(因为它是该地址的
address\u ELEMENT\u POS
),在索引2处设置地址
2
,在索引3处设置地址3。由于没有
address\u ELEMENT\u POS=0的地址,列表的索引0将为空


将数据库中地址元素的值减去1,或将以下属性添加到
列表索引
配置元素中:
base=“1”
。请参阅。

向我们显示国家/地区实体的完整代码。感谢您的帮助!这是国家实体的完整代码。只有能手和二传手。hashCode()和equals(Object obj)方法已由Eclipse自动更新。不要将其作为答案添加。不是一个。单击问题底部的编辑链接,并将代码添加到问题中。
COUNTRY_ID      CONTRY_NAME 
1                    "FRANCE"
2                    "HYPERION"
3                    "ATLANTIS"
ADDRESS_ELEMENT_ID      COUNTRY_ID       ADDRESS_ELEMENT_POS    ADDRESS_ELEMENT_NAME        ADDRESS_ELEMENT_MASK    ADDRESS_ELEMENT_LINE
1                       1               1                       "Code Postal"               "\d{5}"                 1
2                       1               2                       "Ville"                     "[A-Z\-]{1,50}"         1
3                       1               3                       "Batiment"                  "[A-F]"                 1
4                       2               1                       "District"                  "[A-Z]{1,20}"           1 
5                       2               2                       "Zone"                      "[A-5]{1,5}"            2
6                       3               1                       "City"                      "[A-Z]{1,9}"            1
package org.devtutos.tuto5.domaine;

import java.util.ArrayList;
import java.util.List;

public class Country {

    private Long id;
    private String name;
    private List<AddressElement> addressElementList = null;

    public Country(){}

    public Long getId() {
        return id;
    }  

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   

    public void addAddressElement( int pos, int line, String name, String mask){
        addressElementList.add(new AddressElement(pos,line,name,mask));
    }

    public List<AddressElement> getAddressElementList() {
        return addressElementList;
    }

    public void setAddressElementList(List<AddressElement> addressElementList) {
        this.addressElementList = addressElementList;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
             return false;
        if (getClass() != obj.getClass())
            return false;
        Country other = (Country) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}