Please have a look at the below code.
String textName=request.getParameter("textName");
String textadr1=request.getParameter("textadr1");
String textadr2=request.getParameter("textadr2");
String textcntry=request.getParameter("textcntry");
String textregNo=request.getParameter("textregNo");
AgentContact agentContact=new AgentContact();
AgentContact agentContact2=new AgentContact();
Agent agent=new Agent();
agent.setName(textName);
agent.setRegistrationNumber(textregNo);
agent.setDateOfDealStart(textDateDealing);
agent.setRegistrationDate(textregDate);
session.save(agent);
session.save(agentContact);
session.save(agentContact2);
session.getTransaction().commit();
In the above code, we get the values from a form, that is why request.getParameter() is used. However, user may or may not fill some fields like name, so the txtName will be empty.
However when this code is executed, I noticed Hibernate added "empty" string to these non specified Strings, instead of entering null. How can I set the code so it enters null values when the text are actually empty?
This is not a Hiberate issue.
See here:
Are empty fields in form of JSP null or ""?
You simply need to check for an empty String and act accordingly:
agent.setName(textName.equals("") ? null : textName);
The above is a somewhat simplistic solution however: what if the user enters a space? You can use methods of Apache Commons StringUtils for more robust checking:
e.g.
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isBlank(java.lang.CharSequence)
agent.setName(StringUtils.isBlank(textName) ? null : textName);
Related
I have a situation where i have couple of fields i have to pass while calling the cosmos DB , but those fields may not always have values. Some of them might be null while passing them to the repository method. I am trying to do it like below
public interface CachedRepository{
#Query(value="select * from abc a where (#base=null or a.base=#base) and (#position=null or a.position=#position) and (#active=null or a.active = #active)")
List<BackList> getBackListOptions(#Param("base")String base,#Param("position")String position,#Param("active") String active);
The implementation class
latestDetails = repositoryA.getBackListOptions(p.getBase(),p.getPosition(),p.getActive().get(0));//active is a List and we are passing one value
I am trying to pass the request without the active parameter(i.e. active is null in the request) The exception i am getting is
Can not invoke "java.util.List.get(int)" because the return value of request.Pick.getActive() is null
The cosmos Table is
{
'_id":"a25777-j"
"empId": 2436,
"base":"JH",
"position":"HG",
"active":"J"
.........
}
And i am taking reference from this answer
How to write dynamic sql in Spring Data Azure Cosmos DB
Please let me know where i am doing it wrong
The code you have given should work fine if either null or an actual value is passed for any of the parameters. But in your case, for active, it is neither. Nothing is passed, not even null.
It seems from the comments in your code that "active" is an object of type List, and you are attempting to pass a value in that list to getBackListOptions, and extracting that value directly in the method call with p.getActive().get(0). But I think your implementation code is failing where p.getActive().get(0) is called, before getBackListOptions is even invoked. The error you are getting is not something that is returned from Cosmos Spring client, which I believe is doing what it should. You need to handle the values you are passing properly before passing them. I think you could handle this something like below.
String base = p.getBase(); //set the value here so it is clear where failure is
String position = p.getPosition(); //set the value here so it is clear where failure is
String active = p.getActive() == null ? null : p.getActive().get(0); //I think this line was failing before because p.getActive() was null, but this condition handles that case
latestDetails = repositoryA.getBackListOptions(base,position,active);//active is a List and we are passing one value
I am using hamcrest for first time, i have developed a framework to validate particular JSON field values need to satisfy some business rules, i am trying to check on of the field in JSON should not be " " or null. Please find below for the code,matcherOperator validate JSON field against a Array of values for example in ISINARRAY validate the JSON filed is present in the following list of array values [1,2,3,4] similarly NOTINARRAY validate the JSON filed should not be present given range of array values. I am trying to check whether given field is empty or null i defined array like this [null,""] and using NOTINARRAY to check JSON key value shouldn't be null or "" for null its working but the JSON value is "" its validating to true and i tried using Matchers.blankOrNullString() too but its not working and i saw two question of similar type but they are part of JUNIT testing which i tried to used but not working below are the question i referred
Hamcrest matcher with slashes is interpreted as a part of validation
Hamcrest check if value is null or empty array
public <T extends Comparable<T>> Matcher<String> mymatcher(final FieldOperator matcherOperator,
final List<String> value)
{
switch (matcherOperator) {
case ISINARRAY: {
return Matchers.in(value);
}
case NOTINARRAY: {
return Matchers.not(Matchers.in(value));
}
}
I am not sure where i am doing wrong please help me in solving the issue.
Will I receive an error (Exception) on some devices if I set the second parameter of SharedPreferences.getString NULL?
SharedPreferences settings = ...
String data = settings.getString(_.PREFIX , null);
Will it cause an exception or an error on at least one device? Or I have to wrap this part of code in try-catch block?
If you are asking if you will get an exception if you set the second parameter to null, the answer is no (at least not unless you reference the result without first checking it is not null). The second parameter in the getString() method is the default value (i.e. the value that will be returned if there is nothing found for your prefix. So, it is perfectly acceptable to set null as your default value, as long as you realize (and account for) the fact that the value returned by your getString() could be null.
String data = settings.getString(_.PREFIX , null/Null here is default value/);
null - u can receive when your SraredPreferences have not this item(For example if u call/get this string before setting to this field any info or user clear cash of application from settings of device). I think it's can be normal situation, and u can remove "null" with some default value if you hope to got it(some emum field).
If u don't suppose get null validate data before using.
I thin'k your app must be ready get both variant, because user can change normal workflow.
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
I have a native facet script that checks if a specific field (mapped to type long) in the document is empty, this is how I do it:
Object fieldValue = doc().get("fieldName");
return fieldValue == ScriptDocValues.EMPTY;
However, for some of the documents this returns false even when the field is empty (I've checked this with the exists filter). This behavior is inconsistent and it usually returns the correct result. Furthermore, the same document in a different host with the same mapping, same version and same code - returns the correct result.
Is there a better way to check if a field is empty?
I'm using ElasticSearch 0.90.5 with facet script 1.1.2 and java 1.7u17.
The correct way to check for an empty field is:
ScriptDocValues value = (ScriptDocValues) doc().get("field2");
return value.isEmpty();