Setting value for select element is not showing in UI - java

I am using vaadin select to display a select menu with states.
private Select<States> states = new Select<>();
states.setLabel("State");
states.setItems(facade.stateService().findAllStates());
states.setItemLabelGenerator(States::getName);
Optional<States> state = facade.stateService().findByCode(location.getLocationState());
if (state.isPresent()) {
states.setValue(state.get()); // this is not working
}
I am getting the state value and setting it using states.setValue() but the select is not displaying the updated state. The method is getting called. How to make select the menu selected? Thanks.

It's not possible to tell based on the code you have shared. Quite likely the States object returned by location.getLocationState().get() does not share the same object identity with a value returned by facade.stateService().findAllStates(). This can happen even if the properties (such as the "name") of the objects are the same. Typically the answer is to implement the hashCode and equals methods in the class in a way that identity depends only on the id of the entity.

Related

Trying to update transient variable in a hibernate data object for use in a controller, but it's not displaying (in spring mvc)

I'm using hibernate to pull data from two different tables and use them to display. Table A has personnel data, and data B has some state-changing data. Rather than use two lists to display, I want to take a variable from B, apply it to entries pulled from A via a transient variable, and use it in a controller for display.
What I'm seeing is that when I use it in System.out, the value is displayed correctly. however, when I step through with the debugger, it's not showing the correct value (ie: a boolean value that I have in there is defaulted as false, but something in Table B sets it to true under certain conditions. Sys out shows the value get reset to true, but when I step through with debugger, I don't see it set to true).
Further, when I pass it to the jsp (via model) to then trigger a checkbox, the boolean is still false, so the checkbox remains unchecked.
I've had issues in the past where hibernate pulls don't work right when used to set another value, due to lazy loading iirc. Any suggestions?

Adding a new row to another table using java in Maximo

I have two tables that I'm working with: KINCIDENT and ASSISTANT. The main one is KINCIDENT and the two are linked using an ID. I'm able to add a new row from the application but I want to do this using Java. I tried the following code but did not work:
MboSetRemote assistSet = MXServer.getMXServer().getMboSet("ASSISTANT",userInfo);
MboRemote newAssist = assistSet.add();
newAssist.setValue("LOCATION",x);
newAssist.setValue("INCNUM",y);
assistSet.save();
I checked to see if the row was added but it wasn't and I also didn't find any new entries in the database either. Am I missing something?
As long as your code is running, you should have seen that new record in the assistant table, but you definitely would not have seen that on the screen. To make the record appear on the screen, you have to know Maximo's "cache" system to get and edit the exact instance of the set that backs the screen, instead of just any instance (or a whole new instance like you created there).
I don't know where your Java code is (an app bean, an MBOSet, an MBO, or a field class) and I don't know what event/trigger you are hooking into (adding a new record, saving an existing record or something else), both of which are important to know. I will assume you are in an MBO class of the KINCIDENT object running in the "add()" method; meaning when a new KINCIDENT is created, you want to add a new ASSISTANT record. Running as part of that trigger should mean that you are already hooked into the screen instance of the KINCIDENT object when a user adds a new record. To make your ASSISTANT record appear in the set instance backing the screen, you need to the follow the screen's relationships from KINCIDENT to ASSISTANT. I'm assuming on the screen the ASSISTANT table is set up as a child of the KINCIDENT table using a relationship. In that case you just need to get the ASSISTANT set using that relationship. Assuming your relationship is named the same as the set ("ASSISTANT"), it would look something like this:
MboSetRemote assistSet = getMboSet("ASSISTANT");
MboRemote newAssist = assistSet.add();
newAssist.setValue("LOCATION",x);
newAssist.setValue("INCNUM",y);
This will not save your record yet (persist it to the database), but you want to keep your saves to a minimum. Let the user specify if a record should be saved or not by them hitting the "save"/disk icon in the toolbar.

Too many objects match the primary key oracle.jbo.Key

I am getting this error: Too many objects match the primary key oracle.jbo.Key when I tried to search in some view object where the primary key is consists of more than one filed.
Use-case:
In Jdeveloper 11.1.2.3 I have Entity object called someTable this table has three fileds A,B and C where the primary key is consist of tow filed A and B.
I created tow different View object from someTable enity object which are someTableVO1 and someTableVO2. Filed A in the view object somtableVO1 is a List Of Value(LOV) and the data is coming form filed A in the someTableVO2.
In application I have a searching page which contain a drop down list for filed A in somTableVO1. Based on some conditions the drop down list values is not static. The value of the drop down list is changeable based on a select query executed on someTableVO1 and it is based on the thried filed C which is NOT part of the primary key.
The page shows the drop down list with the correct values. But, I have a ValeChangeListener method which will be activated when ever the user select some value form the drop down list.
The problem is:
The error Too many objects match the primary key is occur when ever I am executing this method.
Assumption:
I think that the problem is with the multiple fields for the primary key. of curse there is a duplication in part of the primary key ( i.e in one filed A or B) but the combination of A and B is always unique.
Notes:
1. I check the query by running it in SQL Developer and it is not showing any duplicate values.
2. I did not attach code, because I think the problem is not about (how to do) it is about (what to do).
I hope the idea is clear
Problem Solved.
My aim was to show one filed which is A form someTableVO1 as a list of value and I was trying to achieve that by creating list of value form the same Entity object. I figure out that this was a wrong approach (Correct me if I am worng). to achieve that in ADF technology I should do the following:
Simply drag and drop the view object from the Data Controls to the page.
List of options will appear asking how you would like to show the view object like (Form, Graph,Table,Single selection,....).
3.Select Single Selection -> ADF one Selection
4.Select the attribute(s) you want to be shown to user as a list of values.
That's it...

getting a fresh data object in Apache Cayenne

I want each action taken on certain tables to be logged. I want logging at column level (not all but some) so if a value has been changed for certain column I would like to log that for eg.
Price for product x has been changed by user U
(assume price & product are in the same table.)
for this I want to monitor price column of product x.
I cannot use a trigger to do this as I want the user to be logged as well, User information atm is the in the portal application (can't pass that to the trigger).
I am currently using apache cayenne and on the pre-update (in the entity class) call back I want to compare the new price (which the user has chosen in portal) to the one sitting in Database
when I try to get the product from database, cayenne does not returns me a fresh object, rather returns the same object with changed values
I was wondering if anyone is aware of some way Cayenne can return me fresh object for the same pk(id) (thats what I am using to get a fresh object from the db)
or
can advice me some other way
There are a few ways to approach this. Here is the one that IMO is the most transparent. The trick is to use a different ObjectContext from the one committing changes. Then you will get a separate copy of the object that will contain currently saved value:
// 'this' refers to the DataObject being committed (assuming things happen in its callback)
ObjectContext parallelContext = ... // create a new context here like you normally would
// 3.1 API; 3.0.x has a similar method with a slightly different sig
MyClass clone = parallelContext.localObject(this);
// if you are ok with cached old value, ignore the 'invalidateObjects' call.
// If not, uncomment it to ensure the object gets refetched.
// Also 3.1 API. Should be easy to adjust for 3.0
// parallelContext.invalidateObjects(clone);
Object oldValue = clone.getXyz();

Android/ORMLite Insert Row with ID

I'm currently using ORMLite to work with a SQLite database on Android. As part of this I am downloading a bunch of data from a backend server and I'd like to have this data added to the SQLite database in the exact same format it is on the backend server (ie the IDs are the same, etc).
So, my question to you is if I populate my database entry object (we'll call it Equipment), including Equipment's generatedId/primary key field via setId(), and I then run a DAO.create() with that Equipment entry will that ID be saved correctly? I tried it this way and it seems to me that this was not the case. If that is the case I will try again and look for other problems, but with the first few passes over the code I was not able to find one. So essentially, if I call DAO.create() on a database object with an ID set will that ID be sent to the database and if it is not, how can I insert a row with a primary key value already filled out?
Thanks!
#Femi is correct that an object can either be a generated-id or an id, but not both. The issue is more than how ORMLite stores the object but it also has to match the schema that the database was generated with.
ORMLite supports a allowGeneratedIdInsert=true option to #DatabaseField annotation that allows this behavior. This is not supported by some database types (Derby for example) but works under Android/SQLite.
For posterity, you can also create 2 objects that share the same table -- one with a generated-id and one without. Then you can insert using the generated-id Dao to get that behavior and the other Dao to take the id value set by the caller. Here's another answer talking about that. The issue for you sounds like that this will create a lot of of extra DAOs.
The only other solution is to not use the id for your purposes. Let the database generate the id and then have an additional field that you use that is set externally for your purposes. Forcing the database-id in certain circumstances seems to me to be a bad pattern.
From http://ormlite.com/docs/generated-id:
Boolean whether the field is an auto-generated id field. Default is false. Only one field can have this set in a class. This tells the database to auto-generate a corresponding id for every row inserted. When an object with a generated-id is created using the Dao.create() method, the database will generate an id for the row which will be returned and set in the object by the create method. Some databases require sequences for generated ids in which case the sequence name will be auto-generated. To specify the name of the sequence use generatedIdSequence. Only one of this, id, and generatedIdSequence can be specified.
You must use either generatedId (in which case it appears all ids must be generated) or id (in which case you can set them) but not both.

Categories