Issue while reading solr indexed data - solr

I have indexed pojos in solr successfully but while reading from solr my output is wrong.
Maybe the way I am printing is not correct. Can anyone tell me how to solve this issue?
My code is as follows:
List<SampleDocument> foundDocuments = response.getBeans(SampleDocument.class);
for(SampleDocument docs:foundDocuments) {
System.out.println(docs.getTitle());
}
After printing the output I get is all Null.
SampleDocument.java
package solrobj.Asolrobj;
import java.util.List;
import org.apache.solr.client.solrj.beans.Field;
public class SampleDocument {
private int id;
private String title;
public SampleDocument() {
// required for solrj to make an instance
}
public SampleDocument(int id, String title) {
this.setId(id);
this.title = title;
}
public String getTitle() {
return title;
}
#Field("title")
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
#Field("tid")
public void setId(int id) {
this.id = id;
}
}
Schema.xml
<field name="tid" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
error

Related

Lucene how to search on multivalued field?

I created multivalued field in schema.xml:
<field name="path" type="pint" multiValued="true" indexed="true" stored="true"/>
I created my own search component class like this:
public class CustomComponent extends SearchComponent {
private static final Logger LOG = LoggerFactory.getLogger(CustomComponent.class);
#Override
public void prepare(ResponseBuilder rb) throws IOException {
}
#Override
public void process(ResponseBuilder rb) throws IOException {
LOG.info("CustomComponent running ---");
SolrParams params = rb.req.getParams();
CoreContainer coreContainer = rb.req.getCore().getCoreContainer();
SolrCore solrCore = coreContainer.getCore("example_core");
SolrIndexSearcher categorySearcher = solrCore.getSearcher().get();
IndexReader categoryReader = categorySearcher.getIndexReader();
String pathId = params.get("pathId"); //3
FieldType path = solrCore.getLatestSchema().getField("path").getType();
StandardQueryParser standardQueryParser = new StandardQueryParser();
standardQueryParser.setAnalyzer(path.getQueryAnalyzer());
Query q = standardQueryParser.parse(pathId, "path");
DocList docList = searcher.getDocList(q, null, null, 0, 1000, 1000);
LOG.info(docListsize()); // returns 0
//even if there is a document which has field with such value
}
}
What is wrong here? Is there a way to search on multivalued field? Thanks for answer in advance.

Spring data Solr: IllegalArgumentException - this argument is required

Whenever I try to query Solr using a Spring data repository I get the following exception:
Exception in thread "main" java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.util.Assert.notNull(Assert.java:123)
at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:317)
at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readCollection(MappingSolrConverter.java:423)
at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:331)
at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.readValue(MappingSolrConverter.java:308)
at org.springframework.data.solr.core.convert.MappingSolrConverter$SolrPropertyValueProvider.getPropertyValue(MappingSolrConverter.java:294)
at org.springframework.data.solr.core.convert.MappingSolrConverter.getValue(MappingSolrConverter.java:147)
at org.springframework.data.solr.core.convert.MappingSolrConverter$1.doWithPersistentProperty(MappingSolrConverter.java:134)
at org.springframework.data.solr.core.convert.MappingSolrConverter$1.doWithPersistentProperty(MappingSolrConverter.java:126)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257)
at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:126)
at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:113)
at org.springframework.data.solr.core.convert.MappingSolrConverter.read(MappingSolrConverter.java:88)
at org.springframework.data.solr.core.SolrTemplate.convertSolrDocumentListToBeans(SolrTemplate.java:404)
at org.springframework.data.solr.core.SolrTemplate.convertQueryResponseToBeans(SolrTemplate.java:396)
at org.springframework.data.solr.core.SolrTemplate.queryForPage(SolrTemplate.java:276)
at org.springframework.data.solr.repository.query.AbstractSolrQuery$AbstractQueryExecution.executeFind(AbstractSolrQuery.java:312)
at org.springframework.data.solr.repository.query.AbstractSolrQuery$CollectionExecution.execute(AbstractSolrQuery.java:335)
at org.springframework.data.solr.repository.query.AbstractSolrQuery.execute(AbstractSolrQuery.java:129)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:323)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at $Proxy15.findByTitleStartingWith(Unknown Source)
at foo.bar.Application.main(Application.java:46)
Document:
public class Product {
#Id
#Field
private String id;
#Field
private String description;
#Field
private String title;
// getter/setter
}
Repository:
public interface ProductRepository extends SolrCrudRepository<Product, String> {
List<Product> findByTitleStartingWith(String title);
}
Configuration + Test:
#ComponentScan
#EnableSolrRepositories("foo.bar.repository")
public class Application {
#Bean
public SolrServer solrServer() {
return new HttpSolrServer("http://localhost:8983/solr");
}
#Bean
public SolrTemplate solrTemplate(SolrServer server) throws Exception {
return new SolrTemplate(server);
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
ProductRepository repository = context.getBean(ProductRepository.class);
Product product = new Product();
product.setTitle("Foo title");
product.setDescription("Bar description");
product.setId(UUID.randomUUID().toString());
repository.save(product);
List<Product> list = repository.findByTitleStartingWith("Foo"); // <-- error
System.out.println("result: " + list);
}
}
I Found the solution myself:
I was using the default field configuration for Solr (schema.xml). This configuration includes the fields title and description by default:
<field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="description" type="text_general" indexed="true" stored="true"/>
By default title is a multiValued field. This caused an error when Spring data tried to map the fields back to my Product class.
Removing multiValued="true" (or setting it to false) from the title field solved the problem.

org.apache.solr.client.solrj.beans.BindingException

I have the below bean class for defining my solrInputDocument
public class VenueDocumentSolr extends SolrInputDocument {
#Field
private int id;
#Field
private String uid;
...
}
And I try to get the results using the below code:
SolrQuery query = new SolrQuery();
query.setQuery(SearchForRestaurants);
QueryResponse rsp = SolrUtil.issueSolrQuery(query);
for (SolrDocument s : rsp.getResults())
System.out.println(s);
List<VenueDocumentSolr> beans = rsp.getBeans(VenueDocumentSolr.class);
the above code works sometimes and throws the below Exception rest of the time.
I cross checked and added missing fields to my bean class. But of no use. I still get the error :(
org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class com.zvents.common.entities.solr.VenueDocumentSolr
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:68)
at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBeans(DocumentObjectBinder.java:47)
at org.apache.solr.client.solrj.response.QueryResponse.getBeans(QueryResponse.java:480)
at com.zvents.webapp.api.DeleteData.QueryAndUpdateVenuesForSearch(DeleteData.java:117)
.........
Quoting a part of the Schema
<int name="has_images">*</int>
<arr name="cuisine">
<str>*</str>
</arr>
<arr name="venue_type">
<int>*</int>
</arr>
<double name="location_0_latLon">*</double>
<float name="venue_imp">*</float>
<date name="last_indexed">*</date>
Quoting applicable fields from bean class
#Field
private int has_images;
#Field
private List<String> cuisine;
#Field
private List<Integer> venue_type;
#Field
private double location_0_latLon;
#Field
private float venue_imp;
#Field
private String last_indexed;
I did it in following way:
DocumentObjectBinder binder = new DocumentObjectBinder();
beanlist = binder.getBeans(PortalBean.class, list);
package hello;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class SearchOnNameController {
#CrossOrigin(origins = "*")
#RequestMapping("search")
public List<PortalBean> searchName(#RequestParam(value = "query", defaultValue = "*") String name) {
String urlString = "http://localhost:8983/solr/kislay";
SolrClient solr = new HttpSolrClient(urlString);
SolrQuery query = new SolrQuery();
// query.addField("name");
query.setQuery("name:" + "*" + name + "*");
PortalBean bean = null;
List<PortalBean> beanlist = new ArrayList<PortalBean>();
try {
QueryResponse response = solr.query(query);
SolrDocumentList list = response.getResults();
DocumentObjectBinder binder = new DocumentObjectBinder();
beanlist = binder.getBeans(PortalBean.class, list);
} catch (SolrServerException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return beanlist;
}
}
The bean is mapped as :
package hello;
import org.apache.lucene.document.TextField;
import org.apache.solr.client.solrj.beans.Field;
import org.apache.solr.schema.StrField;
public class PortalBean {
private String id;
private String name;
private String image;
private String description;
private String branding;
private double rating;
private double setup_fee;
private String transaction_fees;
private String how_to_url;
private String [] currencies;
public String getId() {
return id;
}
#Field("id")
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
#Field("name")
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
#Field("image")
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
#Field("description")
public void setDescription(String description) {
this.description = description;
}
public String getBranding() {
return branding;
}
#Field("branding")
public void setBranding(String branding) {
this.branding = branding;
}
public double getRating() {
return rating;
}
#Field("rating")
public void setRating(double rating) {
this.rating = rating;
}
public double getSetup_fee() {
return setup_fee;
}
#Field("setup_fee")
public void setSetup_fee(double setup_fee) {
this.setup_fee = setup_fee;
}
public String getTransaction_fees() {
return transaction_fees;
}
#Field("transaction_fees")
public void setTransaction_fees(String transaction_fees) {
this.transaction_fees = transaction_fees;
}
public String getHow_to_url() {
return how_to_url;
}
#Field("how_to_url")
public void setHow_to_url(String how_to_url) {
this.how_to_url = how_to_url;
}
public String [] getCurrencies() {
return currencies;
}
#Field("currencies")
public void setCurrencies(String [] currencies) {
this.currencies = currencies;
}
}
Also please note that managed-schema file need to have proper definition of the fields:
example as per my use case:
<field name="branding" type="string"/>
<field name="currencies" type="strings"/>
<field name="description" type="string"/>
<field name="how_to_url" type="string"/>
<field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="image" type="string"/>
<field name="name" type="string"/>
<field name="rating" type="tdouble"/>
<field name="setup_fee" type="tdouble"/>
<field name="transaction_fee" type="string"/>
Unfortunately you do not post all of your pojo and all of your schema, so I can only guess.
The only thing I see, from the code you posted is that you are trying to let solr do some sort of conversion for you.
That is for
#Field
private String last_indexed;
And
<date name="last_indexed">*</date>
Solr will not convert a Date to a String for you, just will not. But without a full schema and the full code of your Bean, I cannot give you a full answer.
Other things I noted from the code you post
why are you putting * within the elements of your schema as far as I know that does not have any effect
why are you extending SolrInputDocument? You do not need to. #Field is designed so that you do not need to subclass anything, but may use your model's POJOs right away.
My error was: BindingException: Could not instantiate object
I added a no variable constructor to the POJO and it started to work.
public class MyItem {
String id;
#Field("id")
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public MyItem() {
}
}
To get to here the only fields in my schema were:
<field name="_version_" type="long" indexed="true" stored="true"/>
<field name="_root_" type="string" indexed="true" stored="false"/>
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="text" type="string" indexed="true" stored="false" multiValued="true"/>
<uniqueKey>id</uniqueKey>
This is the code to dump the ids in the solr.
private void dumpSolr2() throws SolrServerException {
System.out.println("dumpSolr2");
SolrQuery query = new SolrQuery();
query.setQuery( "*:*" ); // get all
query.setFields("id"); // get the id field
query.setStart(0); // row to start on. First row is 0.
query.setRows(10); // number of rows to get at a time.
System.out.println(query.toString());
QueryResponse rsp = _solr.query( query );
List<BulkLoad1Data> bulkLoad1DataList = rsp.getBeans(BulkLoad1Data.class);
RplHelper1.rplSay("bulkLoad1DataList.size(): " + bulkLoad1DataList.size());
int count = 0;
Iterator<BulkLoad1Data> i = bulkLoad1DataList.iterator();
while (i.hasNext()) {
BulkLoad1Data bld = (BulkLoad1Data) i.next();
RplHelper1.rplSay("dumpSolr2 - count: " + ++count + " " +
" id: " + bld.getId());
}
}

How to create bind multiple nodes within a single node in SOLR schema file

Iam using SOLR as my search query database.
I am facing difficulty in binding multiple nodes (a collection) within a node in SOLR. Can any body tell me the schema for the same.
My Document is as follows
namespace SOLRApp
{
public class Table1Document
{
public Table1Document()
{
MobileNos = new List<int>();
EducationalDetails = new List<EducationalDetails>();
}
[SolrField("FirstName")]
public string FirstName { get; set; }
[SolrField("MobileNos")]
public List<int> MobileNos { get; set; }
[SolrField("EducationalDetails")]
public List<EducationalDetails> EducationalDetails { get; set; }
}
public class EducationalDetails
{
public EducationalDetails()
{
Details = new List<Details>();
}
public List<Details> Details { get; set; }
}
public class Details
{
[SolrField("IntitutionName")]
public string IntitutionName { get; set; }
[SolrField("EnrollDate")]
public DateTime EnrollDate { get; set; }
[SolrField("PassoutDate")]
public DateTime PassoutDate { get; set; }
[SolrField("InstituteRating")]
public int InstituteRating { get; set; }
}
}
And my schema file is as shown below.
<field name="FirstName" type="string" indexed="true" stored="true"/>
<field name="MobileNos" type="string" indexed="true" stored="true" multiValued="true" />
<field name="EducationalDetails" type="EducationalDetails" indexed="true" stored="true" multiValued="true">
<field name="Details" type="string" indexed="true" stored="true" multiValued="true">
<field name="IntitutionName" type="string" indexed="true" stored="true"/>
<field name="EnrollDate" type="date" indexed="true" stored="true"/>
<field name="PassoutDate" type="date" indexed="true" stored="true"/>
<field name="InstituteRating" type="string" indexed="true" stored="true"/>
</field>
</field>
My main question is what would be the type of 'EducationalDetails' node. This gives me error of
Cannot initialize type 'SOLR.EducationalDetails' with a collection initializer because it does not implement 'System.Collections.IEnumerable'.
I would like to know how can we add user defined datatypes in SOLR. Like in my example, 'EducationalDetails' is a user defined datatype (class). Can any one tell me how to add that in SOLR.
Thank you in advance.

association query issue

I'm using mybatis , I encounter a association query issue, pls take a look at table structures first.
DROP TABLE IF EXISTS `comp_items_spec`;
CREATE TABLE `comp_items_spec` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`comp_id` int(11) NOT NULL,
`content_type_id` int(11) NOT NULL,
`in_list_flag` char(1) DEFAULT NULL,
`label` varchar(200) DEFAULT NULL,
`static_flag` char(1) DEFAULT NULL,
`ext1_name` varchar(200) DEFAULT NULL,
`ext1_value` text,
`ext2_name` varchar(200) DEFAULT NULL,
`ext2_value` text,
`ext3_name` varchar(200) DEFAULT NULL,
`ext3_value` text,
`ext4_name` varchar(200) DEFAULT NULL,
`ext4_value` text,
`ext5_name` varchar(200) DEFAULT NULL,
`ext5_value` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;
/*Data for the table `comp_items_spec` */
insert into `comp_items_spec`(`id`,`comp_id`,`content_type_id`,`in_list_flag`,`label`,`static_flag`,`ext1_name`,`ext1_value`,`ext2_name`,`ext2_value`,`ext3_name`,`ext3_value`,`ext4_name`,`ext4_value`,`ext5_name`,`ext5_value`) values (43,22,1,'\0','description','Y','description','description1......',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL),(44,22,3,'\0','static image','Y',NULL,'http://img3.cache.netease.com/cnews/2012/12/6/20121206092637ba11c.jpg',NULL,'501',NULL,'425',NULL,NULL,NULL,NULL);
/*Table structure for table `components_spec` */
DROP TABLE IF EXISTS `components_spec`;
CREATE TABLE `components_spec` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`show_latest_num` int(11) DEFAULT NULL,
`more_link_url` varchar(200) DEFAULT NULL,
`more_link_flag` char(1) DEFAULT NULL,
`open_in_new_flag` char(1) DEFAULT NULL,
`create_date` datetime DEFAULT NULL,
`update_date` datetime DEFAULT NULL,
`creator_sso` varchar(20) DEFAULT NULL,
`updator_sso` varchar(20) DEFAULT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
/*Data for the table `components_spec` */
insert into `components_spec`(`id`,`name`,`show_latest_num`,`more_link_url`,`more_link_flag`,`open_in_new_flag`,`create_date`,`update_date`,`creator_sso`,`updator_sso`) values (22,'Banner',5,'more.blog.ge.com','Y','Y','2012-12-08 21:30:58','2012-12-08 21:30:58','502156886','502156886');
the relationship between components_spec and comp_items_spec is 1:N,comp_items_spec has two kind of data ,dynamic items and static items which are distinguish from column static_flag, it's dynamic items when static_flag='N', and I also define bean ComponentSpec and CompItemSpec to map the entity. pls see the source code from below:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ComponentSpec {
private int id;
private String name;
private int showLatestNum;
private String moreLinkUrl;
private char moreLinkFlag;
private char openInNewFlag;
private Date createDate;
private Date updateDate;
private String creatorSSO;
private String updatorSSO;
private List<CompItemSpec> staticItemSpecList =new ArrayList<CompItemSpec>();
private List<CompItemSpec> dynamicItemSpecList =new ArrayList<CompItemSpec>();
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public char getMoreLinkFlag() {
return moreLinkFlag;
}
public void setMoreLinkFlag(char moreLinkFlag) {
this.moreLinkFlag = moreLinkFlag;
}
public String getMoreLinkUrl() {
return moreLinkUrl;
}
public void setMoreLinkUrl(String moreLinkUrl) {
this.moreLinkUrl = moreLinkUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getShowLatestNum() {
return showLatestNum;
}
public void setShowLatestNum(int showLatestNum) {
this.showLatestNum = showLatestNum;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public char getOpenInNewFlag() {
return openInNewFlag;
}
public void setOpenInNewFlag(char openInNewFlag) {
this.openInNewFlag = openInNewFlag;
}
public List<CompItemSpec> getStaticItemSpecList() {
return staticItemSpecList;
}
public void addStaticItemSpec(CompItemSpec compStaticItemSpec){
getStaticItemSpecList().add(compStaticItemSpec);
compStaticItemSpec.setComponentSpec(this);
}
public List<CompItemSpec> getDynamicItemSpecList() {
return dynamicItemSpecList;
}
public void addDynamicItemSpec(CompItemSpec dynamicItemSpec){
getDynamicItemSpecList().add(dynamicItemSpec);
dynamicItemSpec.setComponentSpec(this);
}
public String getCreatorSSO() {
return creatorSSO;
}
public void setCreatorSSO(String creatorSSO) {
this.creatorSSO = creatorSSO;
}
public String getUpdatorSSO() {
return updatorSSO;
}
public void setUpdatorSSO(String updatorSSO) {
this.updatorSSO = updatorSSO;
}
}
public class CompItemSpec {
private int id;
private int compId;
private int contentTypeId;
private char inListFlag;
private char staticFlag;
private String label;
private String ext1Name;
private String ext1Value;
private String ext2Name;
private String ext2Value;
private String ext3Name;
private String ext3Value;
private String ext4Name;
private String ext4Value;
private String ext5Name;
private String ext5Value;
private ComponentSpec componentSpec;
public int getCompId() {
return compId;
}
public void setCompId(int compId) {
this.compId = compId;
}
public String getExt1Name() {
return ext1Name;
}
public void setExt1Name(String ext1Name) {
this.ext1Name = ext1Name;
}
public String getExt1Value() {
return ext1Value;
}
public void setExt1Value(String ext1Value) {
this.ext1Value = ext1Value;
}
public String getExt2Name() {
return ext2Name;
}
public void setExt2Name(String ext2Name) {
this.ext2Name = ext2Name;
}
public String getExt2Value() {
return ext2Value;
}
public void setExt2Value(String ext2Value) {
this.ext2Value = ext2Value;
}
public String getExt3Name() {
return ext3Name;
}
public void setExt3Name(String ext3Name) {
this.ext3Name = ext3Name;
}
public String getExt3Value() {
return ext3Value;
}
public void setExt3Value(String ext3Value) {
this.ext3Value = ext3Value;
}
public String getExt4Name() {
return ext4Name;
}
public void setExt4Name(String ext4Name) {
this.ext4Name = ext4Name;
}
public String getExt4Value() {
return ext4Value;
}
public void setExt4Value(String ext4Value) {
this.ext4Value = ext4Value;
}
public String getExt5Name() {
return ext5Name;
}
public void setExt5Name(String ext5Name) {
this.ext5Name = ext5Name;
}
public String getExt5Value() {
return ext5Value;
}
public void setExt5Value(String ext5Value) {
this.ext5Value = ext5Value;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getContentTypeId() {
return contentTypeId;
}
public void setContentTypeId(int contentTypeId) {
this.contentTypeId = contentTypeId;
}
public char getInListFlag() {
return inListFlag;
}
public void setInListFlag(char inListFlag) {
this.inListFlag = inListFlag;
}
public char getStaticFlag() {
return staticFlag;
}
public void setStaticFlag(char staticFlag) {
this.staticFlag = staticFlag;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public ComponentSpec getComponentSpec() {
return componentSpec;
}
public void setComponentSpec(ComponentSpec componentSpec) {
this.componentSpec = componentSpec;
}
}
sqlmap is like below:
<mapper namespace="com.ge.dao.ComponentSpecMapper">
<resultMap id="componentMap" type="componentSpec">
<id column="id" property="id"/>
<result column="temp_id" property="tempId"/>
<result column="show_latest_num" property="showLatestNum"/>
<result column="more_link_url" property="moreLinkUrl"/>
<result column="more_link_flag" property="moreLinkFlag"/>
<result column="open_in_new_flag" property="openInNewFlag"/>
<result column="update_date" property="updateDate"/>
<result column="create_date" property="createDate"/>
<result column="updator_sso" property="updatorSSO"/>
<result column="creator_sso" property="creatorSSO"/>
<collection property="staticItemSpecList" ofType="itemSpec">
<id column="static_item_id" property="id"/>
<result column="id" property="compId"/>
<result column="static_content_type_id" property="contentTypeId"/>
<result column="static_label" property="label"/>
<result column="static_ext1_value" property="ext1Value"/>
<result column="static_ext2_value" property="ext2Value"/>
<result column="static_ext3_value" property="ext3Value"/>
<result column="static_ext4_value" property="ext4Value"/>
<result column="static_ext5_value" property="ext5Value"/>
</collection>
<collection property="dynamicItemSpecList" ofType="itemSpec">
<id column="dynamic_item_id" property="id"/>
<result column="id" property="compId"/>
<result column="dynamic_content_type_id" property="contentTypeId"/>
<result column="dynamic_in_list_flag" property="inListFlag"/>
<result column="dynamic_label" property="label"/>
<result column="dynamic_ext1_value" property="ext1Value"/>
<result column="dynamic_ext2_value" property="ext2Value"/>
<result column="dynamic_ext3_value" property="ext3Value"/>
<result column="dynamic_ext4_value" property="ext4Value"/>
<result column="dynamic_ext5_value" property="ext5Value"/>
</collection>
</resultMap>
<sql id="compSpecMain">
SELECT
comp.id,
comp.name,
show_latest_num,
more_link_url,
more_link_flag,
open_in_new_flag,
create_date,
update_date,
creator_sso,
updator_sso,
si.id static_item_id,
si.content_type_id static_content_type_id,
si.in_list_flag static_in_list_flag,
si.label static_label,
si.ext1_value static_ext1_value,
si.ext2_value static_ext2_value,
si.ext3_value static_ext3_value,
si.ext4_value static_ext4_value,
si.ext5_value static_ext5_value,
di.id dynamic_item_id,
di.content_type_id dynamic_content_type_id,
di.in_list_flag dynamic_in_list_flag,
di.label dynamic_label,
di.ext1_value dynamic_ext1_value,
di.ext2_value dynamic_ext2_value,
di.ext3_value dynamic_ext3_value,
di.ext4_value dynamic_ext4_value,
di.ext5_value dynamic_ext5_value
FROM components_spec comp
LEFT JOIN comp_items_spec si ON si.comp_id=comp.id AND si.static_flag='Y'
LEFT JOIN comp_items_spec di ON di.comp_id=comp.id AND di.static_flag='N'
</sql>
<select id="selectComponentSpecByID" parameterType="int" resultMap="componentMap">
<include refid="compSpecMain"/>
WHERE
comp.id=#{id}
</select>
<select id="selectComponentSpecByName" parameterType="string" resultMap="componentMap">
<include refid="compSpecMain"/>
WHERE
comp.name like #{name}
</select>
the issue here is when I do the selectComponentSpecByID query, the dynamic item list is supposed has nothing , but actual result is two elements, I think it's a problem with result map definition, but no glue. any suggestions? thanks .
finally I found the solution by myself, because when id property is null ,meaning no records retrieved in the result, mybatis still compare them and treat them as valid result and put them into sub list,that's the reason, to fix it,just define notNullColumn of Collection element preventing null id. Done!
<resultMap id="componentMap" type="componentSpec">
<id column="id" property="id"/>
<result column="temp_id" property="tempId"/>
<result column="show_latest_num" property="showLatestNum"/>
<result column="more_link_url" property="moreLinkUrl"/>
<result column="more_link_flag" property="moreLinkFlag"/>
<result column="open_in_new_flag" property="openInNewFlag"/>
<result column="update_date" property="updateDate"/>
<result column="create_date" property="createDate"/>
<result column="updator_sso" property="updatorSSO"/>
<result column="creator_sso" property="creatorSSO"/>
<collection property="staticItemSpecList" notNullColumn="static_item_id" ofType="itemSpec">
<id column="static_item_id" property="id"/>
<result column="id" property="compId"/>
<result column="static_content_type_id" property="contentTypeId"/>
<result column="static_label" property="label"/>
<result column="static_ext1_value" property="ext1Value"/>
<result column="static_ext2_value" property="ext2Value"/>
<result column="static_ext3_value" property="ext3Value"/>
<result column="static_ext4_value" property="ext4Value"/>
<result column="static_ext5_value" property="ext5Value"/>
</collection>
<collection property="dynamicItemSpecList" notNullColumn="dynamic_item_id" ofType="itemSpec">
<id column="dynamic_item_id" property="id"/>
<result column="id" property="compId"/>
<result column="dynamic_content_type_id" property="contentTypeId"/>
<result column="dynamic_in_list_flag" property="inListFlag"/>
<result column="dynamic_label" property="label"/>
<result column="dynamic_ext1_value" property="ext1Value"/>
<result column="dynamic_ext2_value" property="ext2Value"/>
<result column="dynamic_ext3_value" property="ext3Value"/>
<result column="dynamic_ext4_value" property="ext4Value"/>
<result column="dynamic_ext5_value" property="ext5Value"/>
</collection>
</resultMap>

Resources