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...
Related
I'm dealing with a legacy database that uses a strange key/ID configuration for one of its tables. It's the table that defines user information. Here are the columns (I've simplified things a little):
ID
Secondary ID
First Name
Last Name
Change Type
All of these columns are part of the key in the database itself and are needed to uniquely identify a row, with one exception. When the Change Type column has a null value then the ID column uniquely identifies a row. This exception is heavily relied on to get a user's name based on their ID. However I need to specify all columns as #Id for hibernate to work correctly with this table ... or do I? Assuming I do, how would I go about also implementing the exception so that objects can be loaded from the database by just the ID? Ideally I'd like to be able to interact with this object as if ID was the only key since in practice that's how it's done in straight SQL by the DBAs.
I have a problem generating entities with Netbeans, if a table has more than one relationship with another table.
I have, for example, a Task table with three references to User table: the user who created the task, the user who have to track the task and the user who is executing it. Every relationship has its name: fk_user_created, fk_user_resp, fk_user_do.
Netbeans generate three collections: UserCollection1, UserCollection2 and UserCollection3. Not only the names are unclear but, when I regenerate the entities, they change order.
Is there a way to tell Netbeans to use the foreign key name instead of the table or column name?
I know I can directly write Entities instead of automatically generate them, but this is not the case, I'm testing solutions and different database structures and I keep track manually of every change is not practical.
Thank you
The Foreign Key is used by the database. It is how the DB knows which column from a given table references which other table's column.
When you automatically generate Entities in Netbeans, they use the Table and Column name because that is how it recognizes where the column is referencing.
There is no explicit way of telling Netbeans to use the foreign key names, unless you manually change it. Which I would not recommend as it may lead to compilation conflicts.
I have the following table in my db:
CREATE TABLE document (
id INT PRIMARY KEY AUTOINCREMENT,
productModelId INT NOT NULL,
comment VARCHAR(50),
CONSTRAINT FK_product_model FOREIGN KEY (productModelId) REFERENCES product_model(id),
)
Of course, real table is much more complicated, but this is enough to understand the problem.
Our users want to see the number of the document when they click button "new". So, in order to do that, we have to create object in db and send to client that object. But, there is a problem. We need to know productModelId before we save the object in db. Otherwise we will have an sql exception.
I see two possible variants (both are ugly, really):
To show modal list with product models to user and after that create object in database with productModelId chosen by user.
To create a temporary number and after that to save the object in db when user finishes editing the document and saves id. We also need to remove NOT NULL case and validate this somwhere in code.
The first way is bad because we have too much modals in our application. Our UI is too heavy with them.
The second variant is ugly because our database is not consistent without all the checks.
What can you suggest we do? Any new solutions? What do you do in your apps? May be some UI tips. We are using the first variant at the moment.
Theory says that the id you use on your database should not be a relevant information, so the user should not see it if not well hidden in an URL or similar, so you should not display it to the user, and the problem you have is one possible confirmation of this theory.
Right now the solution you have is partially correct: it satisfies technical requirements, but is still bad because if the user doesn't complete the insert you'll end up with the DB having empty records (meaning, with ID and foreign key ok, but all other fields empty or with useless default values), so you are basically circumventing the database validations.
There are two better solutions, but both require you to review your database.
The first is not to use the id as something to display to the user. Use another column, with another "id", declare it unique on the database, generate it at application, display it to the user, and then use this other "id" (if it's unique, it is effectively an id) wherever needed.
The second one is the one that is being used often cause it does not require a central database or other authority to check uniqueness of ids, so scales better in distributed environments.
Drop the use of the common "id int" auto-incremented or not, and use UUIDs. Your id will be a varchar or a binary, an UUID implementation (like java.util.UUID, but you can find in other languages) will generate a unique id by itself whenever (and wherever, even on the client for example) you need it, and then you supply this id when saving.
We make it the following way.
Created table id_requests with fields issue_type_id and lastId. We need this in order to avoid the situation when two users hit the button 'new' and get the same ids.
And of course we added field innerNum to all the tables we use this feature in.
Thank you!
I have a SQL Server database that holds a table where a varchar column has a check constraints on it to make sure only a few different words can be entered as a value (names).
Like this
CONSTRAINT chk_Names CHECK (name IN ('John', 'Eva', 'Carl', 'Fred'))
What I want do do is to populate a combobox in java with these names, and I don't want to manually enter them since they might change in the database. I want to populate it from metadata.
But I haven't been able to find a way to get the information from the database either with the INFORMATION_SCHEMA or sys.objects (or from DatabaseMetaData in java for that matter)
I'm quite new to SQL Server but is it possible to get that information somehow?
Regards
/Fred
It sounds like you should move the list of names to a table. You're Java form could select the data from the table.
And, because the data can change, it will be better to update the table than to change the check constraint. You can change the check constraint to a foreign key constraint too.
You can also find the check-constraint definitions in INFORMATION_SCHEMA.CHECK_CONSTRAINTS. The expression is in the CHECK_CLAUSE column; and, you'll have to extract the values from the expression.
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.