I have the following table in which I have to set orderID against each itemName, my itemName set successfully but orderId sets null , When I print the value at console it prints right value.
Can anybody tell me what is the problem in my code?
List<String> items;
items=orderRequest.getOrderItem();
OrderItem orderItem=new OrderItem();
for (String temp : items) {
orderItem.setItemName(temp);
orderItem.setOrderId(order.getId());
System.out.println("Order Id " + order.getId());
orderItemRepo.save(orderItem);
}
Please check your field name and also attach a debugger to figure out the real problem
The first thing that comes to my mind is that you are looping over potentially multiple items and setting the order id of the single orderItem every time.
The best way to figure out these kind of problems and get a feeling for what your code is doing is to attach a debugger and step through the code.
Related
I recently work with commerce tools platform and I have such a question.
I have this query:
CompletionStage<List<Category>> stage = QueryExecutionUtils.queryAll(client, CategoryQuery.of().byName(Locale.ENGLISH, "cat1"));
final CompletableFuture<List<Category>> result = stage.toCompletableFuture();
return result.get().get(0);
Is there a way to return just a Category instead of List.get(0) and how it can be done?
Thanks for submitting this question. There is no unique constraint for the name field on categories. For this reason we cannot guarantee that the API will only return one result. With the name query you will get paged results and will have to pull the first entry from the list. Both key and id are both unique so you could query for either of those and get just the unique category. You can view the representation of categories in the docs here. https://docs.commercetools.com/http-api-projects-categories#category
Hope this helps!
I have this bean/table "Userinfo" with columns id, username, and twitchChannel.
For most userinfo the twitchChannel column will be null. I'm going through every userinfo entity in the table and search the column twitchChannel, if the column is not null I put the twitchChannel in an array.
this is what my request looks like:
"SELECT ui FROM Userinfo ui WHERE ui.iduserinfo=:id"
It is very inefficient because I'm going through every single entity even those which have a null twitchChannel and I'm not interested in those.
This is java but I commented every line so it's easy to understand for those who don't know it.
while (true) { // I'm going through the table in an infinite loop
int id = 0; //id that is incremented for searches
Userinfo ui; // this will be an object that will hold the result of my query
do {
ui = ups.getUserInfo(id); // this execute the query I posted above
id++; //incrementing id for next search
if (ui.getTwitch() != null) { // if the search didn't return null
twitchChannels.add(ui.getTwitch()); // put my twitch in an array
}
} while (ui != null);
}
So at the moment I'm going through every entity in my table even those with a null twitch. To my understanding it's possible to speed up the process with indexes.
CREATE INDEX twitchChannel
ON Userinfo (twitchChannel)
So something like that would have a table with not null twitchChannel. How I loop through this table like above ?
Will it work the same way with java persistence?
Change the query to:
SELECT ui
FROM Userinfo ui
WHERE twitchChannel IS NOT NULL
This will benefit from an index on Userinfo(twitchChannel) (assuming there really are very few values that are filled in). At the very least, this reduces the amount of data passed from the database to the application, even if an index is not used.
If I've understood your question correctly. You have a table containing numerical id's. And you are searching the space of real numbers to see if any of those correspond to an id in your table ('twitch' id ?)
Assuming you have less than infinity users, I would have thought you can reverse this logic.
Change your query to :
SELECT iduserinfo FROM Userinfo ORDER BY iduserinfo
Then your java code will be something along the lines of :
uiResult = ups.getUserInfo(id); // this executes the new query
while (uiResult.next()) {
twitchChannels.add(uiResult.getTwitch()); // put my twitch in an array
}
(Apologies, its been a long time since I've used jdbc).
Sorry If I've misunderstood the question.
I need to find the _id of the last upserted document in java. I am using 2.0.5. I do not see an 'upserted' field in the output returned by getLastError if the element was updated. I do see it if the element was inserted. I need to get the _id regardless of whether the element was updated or inserted. Is it possible to get the _id of the document in some way other than issuing another find command? I am trying to reduce the unnecessary query.
You can use DBCollection.findAndModify(). You can set it's parameters such that it will perform the upsert and return a DBObject containing the _id of the object you just created/modified.
Check out the docs for a lot more info:
http://www.mongodb.org/display/DOCS/findAndModify+Command
ADDENDUM: I just revisited my answer and realize that there's a better, easier way. The value of _id is always set client-side, before the document is sent over the wire to the server. The driver does this for you, if you haven't assigned a value to the _id already. Since the client sets the _id, you know what it is even before the insert operation start. consider this example.
> var id = ObjectId()
> id
ObjectId("5511062d729ddce46b99ea3f")
> db.test.insert( { _id: id, text:"a trivial experiment"})
WriteResult({ "nInserted" : 1 })
> db.test.find( { _id: id} )
{
"_id" : ObjectId("5511062d729ddce46b99ea3f"),
"text" : "a trivial experiment"
}
Following Code Will Give You last Modified record:
db.products.find().sort({"$natural":1}).limit(1);
Following Codes Will Give You last Inserted record:
db.products.find().sort({"_id":-1}).limit(1);
db.products.find().sort({"$natural":-1}).limit(1);
I have a strange problem to update a table in my database...forgive me if I can not explain well but I'm a bit confused...
The problem is this:
I created a table with values, I read this values in my listview..everything works for now..insert and delete values works without problem..now created a loop in a service why do I need to make a comparison between a value and a string of my database and when this comparison is true, I need to change a value in my table..
The real problem is this: my db.update works only if not use ... never, the command db.delete... if use it, the db.update not work anymore ..and to make it work again, i need to make a new AVD.
how is it possible?
my db.delete and id is this:
item.getMenuInfo();
id = getListAdapter().getItemId(info.position);
public void deleteReg(SQLiteDatabase db ,long id)
{
db.delete(TabRegistry.TABLE_NAME, TabRegistry._ID + "=" + id, null);
}
on activity:
databaseHelper.deleteReg(db, id);
my db.update is this: (positions is a value of getPositions(),for locate a positions with a cursor(always works, even when fails db.update))
public void updateReg(SQLiteDatabase db,int positions, String stat)
{
ContentValues v = new ContentValues();
v.put(TabRegistry.STATUS, stat);
db.update(TabRegistry.TABLE_NAME, v, TabRegistry._ID + " = " + positions, null);
}
on service:
databaseHelper.updateReg(db, positions, "SUCCESS");
if you need more code, tell me what I add now..thanks in advance
The SQLite api you are using is based off of CRUD operations (you should read this).
You are DELETE-ing the record from the database, therefore there is nothing to UPDATE when you attempt to update it. If you want to create a new record, or recreate the one you deleted then you would perform an INSERT instead of an UPDATE.
EDIT:
It also appears you are passing in position number to the update and delete. I assume that you are also using this value to place the record in your table? Is it possible that when you delete the record from the table and the database, that the other records now have an invalid position because they haven't been updated also? It's just a shot in the dark, figured I might as well ask.
I need to call a Stored Procedure running in DB2.Since the Oracle DB Adapter Doesnt support DB2 , I have used Java Embedding to call the Stored Procedure . I Dont have a problem in Calling the Stored procedure using Java Embedding and getting the result Set.The problem starts when is start assinging the result set to the output variable .I use the below snipped to set the variable.
rset=statement.executeQuery("SELECT name, number, salary from Employee");
rset = statement.getResultSet();
int j=1;
if (rset != null)
{
while(rset.next())
{
name=rset.getString("name");
name=rset.getString("number");
name=rset.getString("salary");
setVariableData("temp","payload","/ns1:code1["+j+"]/ns1:empname",name);
setVariableData("temp","payload","/ns1:code1["+j+"]/ns1:empno",number);
setVariableData("temp","payload","/ns1:code1["+j+"]/ns1:salary",salary);
j=j+1;
}
}
In the above mentioned Code I expect to display all the empname,number and salary details of all the employees to be assigned to output variable temp .But the output i see is only the last employee details .The remainin employee details are not shown .If i replace the variable j with either 1 or 2 or 3 etc i get the output of only that array element details whereas other details are not known .Can someone help me in getting the whole result set to a output variable .
Have you tried to create the "/ns1:code1["+j+"] element before setting is sub element ?
I foudn this issue report :
http://oraclefusion1011.blogspot.com/2011/01/issue-with-setvariabledata-in-java.html