I have code like this for getting all data from my DB
Iterable<ProductParameter> result = service.getAllProductParameter();
I need to get value from one of productParameter Entity, how to get them? if I use iterable to get all data in my DB`
I have tried using this code
String uuid = result.getProductParameterId; but this got error
You should explain more details. But
Iterable<ProductParameter> result = service.getAllProductParameter();
return a list of object so you need to filter or loop to get what you need.For example:
result.forEach(x -> {System.out.println(x.getProductParameterId());});
Related
I have a Set, it's generic type is Set. I get it from this code
Set<Integer> completeOrgTree = codeSetOfStores.stream().flatMap(item -> organizationDao.getUpstreamOrgByCode(item).stream()).collect(toSet());
the organizationDao.getUpstreamOrgByCode method is simple:
public Set<Integer> getUpstreamOrgByCode(String code){
return organizationMapper.getUpstreamOrgByCode(code);
}
codeSetOfStores is a normal Set,i get it from
Set<String> codeSetOfStores = organizationDao.listOrganizations(OrganizationQuery.getInstance().storeIdList(new ArrayList<>(searchBean.getStoreIdSet())).fields("code").turnOffPage())
.stream().map(OrganizationEntity::getCode).collect(toSet());
//对于业务组织,获取关联该批分店的组织
Set<String> codeSetOfAssociatedStore = orgStoreAssociatedDao.listOrgStoreAssociated(OrgStoreAssociatedQuery.getInstance().turnOffPage().withStoreIdSet(searchBean.getStoreIdSet()).withFields("orgCode"))
.stream().map(OrgStoreAssociatedEntity::getOrgCode).collect(toSet());
//集合业务组织和非业务组织,查询该批组织及其上级
codeSetOfStores.addAll(codeSetOfAssociatedStore);
I have described the background of completeOrgTree above.
My problem is that when want to operate the completeOrgTree, I found that this container filled with String !!!
So every operation about this container will cause a ClassCastException,even what i do just print it
completeOrgTree.stream().forEach(item -> System.out.println(item));
My JDK version is 1.8.0_131.
========================================================================
i found the answer.
what make this happen is the mybatis.
in the xml file , i have this methd
<select id="getUpstreamOrgByCode" resultType="String">
SELECT
id,
`CODE`
FROM
t_org_architecture
WHERE
LEFT (#{code}, LENGTH(`CODE`)) = `CODE`;
</select>
the resultType of this select is String.
int the java file , i have this corresponding method:
Set<Integer> getUpstreamOrgByCode(String code);
so,the mybatis put the string value into the set in spite of the type of this set is integer!!!
i guess mybatis do this by java reflection
You are a victim of non-reified generics. At runtime, it is perfectly possible to put any object into a collection, regardless of its declared generic type. Generic types are not enforced at runtime.
Here is an example where I add 3 different types to a List<Integer>, which will also produce a class cast exception when you try to iterate over it.
List<Integer> intList = new ArrayList<>();
List anyList = intList;
anyList.add(1);
anyList.add(new Object());
anyList.add("test");
intList.stream().forEach(item -> System.out.println(item));
So I suppose whoever is responsible for populating the set has screwed up somewhere. You should ideally fix it at the source.
If that's not possible then cast it to a Set<Object> (since we know that that's really what it is, for whatever reason).
List<Object> objList = (List<Object>) (List) intList;
objList.stream().forEach(item -> System.out.println(item));
If you want to do anything more complex than printing each item, you may need to check the type (instanceof) and then add some conditional logic for Strings and Integers.
i found the answer.
what make this happen is the mybatis.
in the xml file , i have this methd
<select id="getUpstreamOrgByCode" resultType="String">
SELECT
id,
`CODE`
FROM
t_org_architecture
WHERE
LEFT (#{code}, LENGTH(`CODE`)) = `CODE`;
</select>
the resultType of this select is String.
int the java file , i have this corresponding method:
Set<Integer> getUpstreamOrgByCode(String code);
so,the mybatis put the string value into the set in spite of the type of this set is integer!!!
i guess mybatis do this by java reflection
//Below code is Not Working
//Here, my query just returns one object, So I am trying to use findOne() //method.
Query<Topic> query = Ebean.find(Topic.class);
Topic topic = new Topic();
Topic topic=Topic.find.where().eq("columnName", "nameToMatch").findOne();
//Below part is working if I use findList(). But I have to do get(0) to //fetch the topic which is not good practice I think.
List<Topic> topicList = Ebean.find(Topic.class).where().eq("columnName", "NametoMatch").findList();
topicList.get(0)
Can anyone provide ideas how to return just One Object instead of list ?
I don't know if findOne exists in Ebean, but when I need to retrieve only one object I use findUnique()
If you're sure the object you want to find is unique, you can get it via findUnique(): Topic.find.where().eq("columnName", "nameToMatch").findUnique();
Otherwise you can use findList() with setMaxRows(), because you don't want to load in memory whole result set:
Topic.find.where().eq("columnName", "nameToMatch").setMaxRows(1).findList();
I want to write a query that gets list of all values of one field in the documents (no conditions at all).
I try the following code:
#Query(fields = { "car_company_s" })
List<Car> findAllCarCompeny();
But it didn't work, I got the following error:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findAllCarCompeny found for type Car!
Is there any way to do this?
What you want is a "Term". Basically it returns all values of a field. You need solr template, as said in description .
Example we are using:
#Resource
private SolrTemplate solrTemplate;
...
query = SimpleTermsQuery.queryBuilder()
.fields(key)
.prefix(startsWith.toLowerCase(Locale.ROOT)) // we have some search prefix
.sort(TermsOptions.Sort.INDEX)
.build();
TermsPage page = solrTemplate.queryForTermsPage(query);
Iterable<TermsFieldEntry> terms = page.getTermsForField(key);
Terms are basically your values. Note, that you have to optimize core to remove values, that are from already deleted documents.
I'm getting distinct values of field from mongodb.
When i run the following query in the commandline, it works good.
db.celldata.distinct("tenentId")
I'm using Mongo java 3.0 driver, to retrieve the distinct values using the following query
MongoCursor<String> iterator = coll.distinct("tenantId", String.class).iterator();
When i run the query i get the following exception
org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is NULL.
Well, the root cause of the error here is because you have a String type as expected output and one of the distinct values is actually null. The way I see it you have two ways of working with this.
Also noting that an "iterator" is overkill for a "distinct" list generally speaking, so just go straight to ArrayList.
Either .filter() out the unwanted null values:
ArrayList<String> distinct = coll.distinct("tenantId", String.class)
.filter(new Document("tenantId",new Document("$ne",null)))
.into(new ArrayList<String>());
System.out.println(distinct);
Or accept the results as BsonValue and handle those:
ArrayList<BsonValue> distinct = coll.distinct("tenantId", BsonValue.class)
.into(new ArrayList<BsonValue>());
System.out.println(distinct);
But in the latter case, you still need to handle the types returned. There are methods on BsonValue to allow you to code for this, but it is also a fair bit of overkill for just getting a list of distinct values.
Therefore as a "third" option, I would go for something with an "un-typed" response. The .aggregate() method works will here, but it will be BSON documents in the response, it is still up to you to transfer to a plain ArrayList of different types:
ArrayList<Object> objects = new ArrayList<Object>();
MongoCursor<Document> iterator = coll.aggregate(Arrays.asList(
new Document("$group",
new Document("_id", "$tenantId")
)
)).iterator();
while (iterator.hasNext()) {
objects.add(iterator.next().get("_id"));
}
System.out.println(objects);
I have custom object in my RemedyForce abc__c and would like to get list of it.
Tried this code:
SearchResult sr = con.search(
"FIND {00008137} IN abc__c FIELDS RETURNING abc__c(Id, Name)");
but it returns
[InvalidSObjectFault [ApiQueryFault [ApiFault
exceptionCode='INVALID_TYPE' exceptionMessage='sObject type
'abc__c' is not supported. If you are attempting
to use a custom object, be sure to append the '__c' after the entity
name. Please reference your WSDL or the describe call for the
appropriate names.'
Tried this code too and it returns:
String sql = "SELECT Id, Name FROM abc__c LIMIT 10";
QueryResult result = con.query(sql);
[InvalidSObjectFault [ApiQueryFault [ApiFault
exceptionCode='INVALID_TYPE' exceptionMessage='sObject type
'abc__c' is not supported.'] row='-1' column='-1'
]]
Anyone can advise how to get list of my custom object?
Your code looks good. This exception can happen if the user making the request doesn't have a permission to the specified object.