Playframework get List of Objects as Options in select - java

I am using Playframework 2.1.1 and Java. I am trying to fill a selectbox with data I retrieve from a database using the Play formhelpers
Here is some code if the view:
#helper.form(action = routes.Admin.submitUnit) {
#helper.select(
field = unitForm("metaunit"),
options = options(Metaunit.find)
)
...
}
And the Method to retrieve Metaunits from db:
public static List<Metaunit> find(){
Query query = JPA.em().createQuery("SELECT e FROM Metaunit e");
return (List<Metaunit>)query.getResultList();
}
When I try to compile it, I get the following Error-Message:
Overloaded method value [apply] cannot be applied to (java.util.List[models.Metaunit])
Any help is appreciated! Thanks

Take a look into computer-database-jpa sample ie. options() method in Company model in general it returns Map<String, String>.
As you can see in the editForm view, usage is pretty similar to your.
Note: probably you Metaunit is connected with some M-M relation, in that case most probably you will need to use unitForm("metaunit.id") as a field's value

Related

Finding a list of related objects by ID

Let's say for example I have a bridge table called PersonAnimal. I want to search for all the people who have a given animal's ID. The query so far looks like:
Animal animal = getById(Animal.class, animalId)
ObjectSelect
.query(PersonAnimal.class)
.where(PersonAnimal.ANIMAL.eq(animal))
.select(context)
However the first line in the above code segment shows that I first have to retrieve the related object from the database. I want to get rid of that database lookup and instead do something like:
ObjectSelect
.query(PersonAnimal.class)
.where(PersonAnimal.ANIMAL_ID.eq(animalId)) // <- Find by ID instead
.select(context)
Is that possible?
I am running version 4.1 of the Apache Cayenne ORM.
Just as I posted the question I found the answer. You need to create an Expression using a Property object like so:
val findByIdExpr = Property.create(PersonAnimal.ANIMAL.name, Long::class.java).eq(yourId)
val gotList = ObjectSelect
.query(PersonAnimal.class)
.where(findByIdExpr)
.select(context)
Above code is in Kotlin but is also easy to understand from a Java perspective.

Regarding better way to Query MongoDB from Java

Disclaimer: I am new to MongoDB. I just started to use it few days back. Sorry if my question doesn't make much sense.
Hello,
I am trying to make a Query to MongoDB from Java method, I want to create and make a query only if that parameter is not null. So let us say that my method is
public List<Object> getSomethingFromMongoDB(String searchParameter){
Query query = new Query().addCriteria(Criteria.where("something").is((searchParameter)));
}
Now I only want to search when SearchParameter only when it is notNull. Can I do this in a better way? I tried to check $ne but cannot understand how to apply it in my Java method.
Thanks in advance
I would advise the following:
public List<Document> getSomethingFromMongoDB(List<String> searchParameters){
List<Document> results = new ArrayList<>();
if (searchParameters == null) return results;
Document criteria = new Document();
searchParameters.forEach(parameter -> criteria.append("yourField",parameter));
collection.find(criteria).iterator().forEachRemaining(results::add);
return results;
}
If the parameters list is empty, you will just get all the documents.
If not, the corresponding criteria will be applied.
You could even get rid of the null-checking line, by making sure you pass a new ArrayList() for example, instead of null.

Unable to retrive Projection/Multi- Relantion field Requests.Custom_SFDCChangeReqID2 using versionone java sdk

I have been trying to retrieve information from querying a specific Asset(Story/Defect) on V1 using the VersionOne.SDK.Java.APIClient. I have been able to retrieve information like ID.Number, Status.Name but not Requests.Custom_SFDCChangeReqID2 under a Story or a Defect.
I check the metadata for:
https://.../Story?xsl=api.xsl
https://.../meta.V1/Defect?xsl=api.xsl
https://.../meta.V1/Request?xsl=api.xsl
And the naming and information looks right.
Here is my code:
IAssetType type = metaModel.getAssetType("Story");
IAttributeDefinition requestCRIDAttribute = type.getAttributeDefinition("Requests.Custom_SFDCChangeReqID2");
IAttributeDefinition idNumberAttribute = type.getAttributeDefinition("ID.Number")
Query query = new Query(type);
query.getSelection().add(requestCRIDAttribute);
query.getSelection().add(idNumberAttribute);
Asset[] results = v1Api.retrieve(query).getAssets();
String RequestCRID= result.getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= result.getAttribute(idNumberAttribute).getValue().toString();
At this point, I can get some values for ID.Number but I am not able to retrieving any information for the value Custom_SFDCChangeReqID2.
When I run the restful query to retrieve information using a browser from a server standpoint it works and it does retrieve the information I am looking for. I used this syntax:
https://.../rest-1.v1/Data/Story?sel=Number,ID,Story.Requests.Custom_SFDCChangeReqID2,Story.
Alex: Remember that Results is an array of Asset´s, so I guess you should be accessing the information using something like
String RequestCRID= results[0].getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= results[0].getAttribute(idNumberAttribute).getValue().toString();
or Iterate through the array.
Also notice that you have defined:
Asset[] results and not result
Hi thanks for your answer! I completely forgot about representing the loop, I was too focus on the retriving information part, yes I was actually using a loop and yes I created a temporary variable to check what I was getting from the query in the form
Because I was getting the variables one by one so I was only using the first record. My code works after all. It was just that What I was querying didn't contain any information of my use, that's why I was not finding any. Anyway thanks for your comment and observations

ElasticSearch get _id

How can I retrieve the _id elasticsearch field information using Java API? I know I can see this information with head plugin, looking to documents, but it's not necessary for me.
I'm developing a Java project, and all commands, like update, delete, require this _id value. But how can I get it?
Thanks in advance
When executing a search, you get back an object SearchResponse. By calling the method getHits() you get back a list of objects of type SearchHit. This object has a method id().
SearchResponse searchResponse = client.prepareSearch().setQuery(matchAllQuery()).get();
for (SearchHit hit : searchResponse.getHits()) {
String yourId = hit.id();
}

Function inside Hibernate Criteria

I'm trying to create a Hibernate criteria filter that uses a database (Oracle) function as part of the Where.
So far, I have it working with sqlRestriction...but I can't seem to ge the {alias} to work (I'm trying to feed in the "id" of the current row as a function argument).
e.g., my function takes 2 arguments:
def query = {
and {
sqlRestriction("F_FGAC('SomeText',{alias}.id) = 'Y'")
}
}
The error I get is:
No signature of method: basecas_05.EmplController.sqlRestriction() is applicable for argument types: (java.lang.String, java.lang.String) values: [F_FGAC('SomeText',{alias}.id) = 'Y'...].
Any idea what the problem is here...?
I see the problem...it's not with the alias, it's that I was not putting the right arguments into the method...this works:
sqlRestriction("F_FGAC('SomeText',{alias}.id) = 'Y'")

Categories

Resources