我对 Hibernate、一对多关联和列表有疑问。在我的模型中,我有国家,对于每个国家,我都有多个描述该国家地址结构的信息。我想访问这些元素的列表。
这是我的班级国家:
public class Country {
private Long id;
private String name;
private List<AddressElement> addressElementList = null;
...
}
我的班级地址元素:
public class AddressElement {
private Long id;
private int pos;
private int line;
private String name;
private String mask;
...
}
我的映射:
<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>
这是我的代码:
criteria = getSessionFactory().getCurrentSession().createCriteria(Country.class);
criteria.add(Restrictions.eq("name", countryName));
criteria.setFetchMode("addressElementList",FetchMode.JOIN);
Country country = (Country) criteria.list().get(0);
if (country == null) {
logger.log(Level.INFO, "ERREUR DE COHERENCE INTERNE : Pays '".concat(countryName).concat("'non trouvé:"));
} else {
logger.log(Level.INFO, "Pays trouvé.");
logger.log(Level.INFO, "Nom du Pays :".concat(country.getName()));
logger.log(Level.INFO, "Code du Pays :".concat(String.valueOf(country.getId())));
liste = country.getAddressElementList();
logger.log(Level.INFO, "Taille de la liste des éléments d'adresse :".concat(String.valueOf(liste.size())));
for (AddressElement elem : liste) {
if (elem == null) {logger.log(Level.INFO, "L'élément d'adresse est null.");}
logger.log(Level.INFO, "Element de la liste : ID = ".concat(String.valueOf(elem.getId()))); **** Line of the error
logger.log(Level.INFO, "Element de la liste : POS = ".concat(String.valueOf(elem.getPos())));
logger.log(Level.INFO, "Element de la liste : NOM = ".concat(elem.getName()));
}
}
这是我的日志:
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 工作):
logger.log(Level.INFO, "Lecture de tous les éléments d'adresse.");
criteria = getSessionFactory().getCurrentSession().createCriteria(AddressElement.class);
liste = criteria.list();
logger.log(Level.INFO, "Taille de la liste des éléments d'adresse : ".concat(String.valueOf(liste.size())));
for (AddressElement elem : liste){
if (elem == null) {logger.log(Level.INFO, "L'élément d'adresse est null.");}
logger.log(Level.INFO, "Element de la liste : ID = ".concat(String.valueOf(elem.getId())));
logger.log(Level.INFO, "Element de la liste : POS = ".concat(String.valueOf(elem.getPos())));
logger.log(Level.INFO, "Element de la liste : NOM = ".concat(elem.getName()));
}
这是这段代码的结果:
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
在第一次执行时为空。有人可以帮助我吗?
提前致谢。
祝新年快乐。;-)