So, in my app I have a pre-populated database (I used SQLiteStudio to add the data and set the primary key on auto-increment for each table).
I want to add more rows in the one table of the database (that has already some data).
The problem is that the primary key is not auto-incremented and I think this happens because there is already some data in the table.
Does anyone know how to make the autogenerate work for pre-populated databases? Or the only solution is to manually add the ID, by selecting max(ID) from the table and adding 1 to it ?
Thank you in advance for taking some time to read this!
One of these tables is Users and I put the autogenerated pk:
#Entity(tableName = "users")
public class Users_table {
#PrimaryKey(autoGenerate = true)
private int userID;
// the rest of the code
}
I also checked and the #Insert method is called but I saw in logs that all userIDs are 0.
Related
I have situation like this.
I need to read data from a table. Let's say this table is called Person_Old and has three columns
I need to read the data from Person_Old and to save in another table called Person_New. In the Person_New table, I need to insert an extra column called ID. So in the Person_New table, I have a total of 4 columns.
The new table should be like this:
I have a Java class Called Person. where i have three fields firstname, lastname and dateOfBirth. Now if i insert another field called id like below
#ID
#GeneratedValue
private Long id;
then I get an error
Invalid column name ID
So now my question is how to insert this id column in my java class and in the SQL script for creating Person_new table so that when I run the program it'll read the data from old table and while persisting data in new table it'll generate Ids for each entry.
I would be grateful for your suggestions. Thank you.
You should define the column name and Primary key annotation need to be corrected.
#Id
#GeneratedValue
#Column("ID")
private Long id;
I'm new to PostgreSQL and Java Swing.
In PostgreSQL company database, I have a users table and it has 4 fields: user_id, username, phone, and address.
CREATE TABLE users
(
user_id serial primary key,
username VARCHAR(40) not null,
phone VARCHAR(14) not null,
address VARCHAR(50)
);
I'm trying to load all fields from users table to JTable in Java Swing using Bound (Figure 1).
Then I bind the elements from the table (Figure 2).
As you can see in Figure 2, it shows only 3 fields except user_id. I need to load this user_id field as well because I need to perform CRUD data.
How can I achieve that?
I finally got the solution.
When I import the data from the database to the form, the Model (for example; Users.java) file is generated.
The problem was I firstly generated that file and later I added the user_id column to the database, so guess what, the Model file is somehow not updated and so the user_id is not there.
Therefore, I had manually added getter and setter for the user_id field, and now ok.
I am learning PostgreSQL, and need help to solve one problem.
I have two java entity classes, say
User
{
Long id;
String name;
Set<Long> bookIds;
}
Book
{
Long id;
String name;
}
When it translated into PostgreSQL DB scheme, it created a user table, user_bookids table and book table.
I want to insert a new row into user table and user_bookids table with bookids selected ids from book table.
I knew how to do it in Java land, I can create a user object and populate the bookIds list and persist, data will be populated to two tables.
but how can I do this in PostgreSQL.
Thanks in advance for all helps.
My application need to store Model_A into SQLITE database, and Model_A include a ArrayList of Model_B as I describe in example code below:
class Model_A {
String name;
String description;
int number;
List<Model_B> list_Model_B;
}
class Model_B {
String title;
int quantity;
}
I searched and know that I can use GSON (or JSONObject) to convert Model_B to a json String file and store this json in Table of Model_A. But this way is not good for handling the data. I just want to do it without using GSON.
I thought about create two table for Model_A and Model_B. Table of Model_A will store a column ID of Model_B. But this way only can store a object Model_B, not a List as I expect.
Can you let me know how to design the database reach my expect ? Or please suggest me a library which can help me on this case. Thanks.
According to relation database theory, you need to build one-to-many relationship, using two tables - the first one for Model_A, the second - for Model_B.
Main idea of this relationship is connect two entities with foreign keys to have possibility for querying.
Let's write some SQL for creating such relationship:
CREATE TABLE model_a (
_id INTEGER PRIMARY KEY,
field TEXT NOT NULL
);
-- Table for Model_B
CREATE TABLE model_b (
_id INTEGER PRIMARY KEY,
model_a_id INTEGER NOT NULL,
field_2 TEXT NOT NULL,
FOREIGN KEY model_a_id REFERENCES model_a(_id)
);
OK, we created two tables. But how can we get List<Model_B> from such scheme?
There are two ways to do it.
The first - make two selects for every Model_A class object: one for "details" of this object from model_a table, and one for list of Model_B elements:
SELECT *
FROM model_a
WHERE _id = ?
-- For list of Model_B
SELECT *
FROM model_b
WHERE model_a_id = ?
The second way - get all information about one Model_A by one query. For this task we need special SELECT query:
SELECT *
FROM model_a ma
INNER JOIN model_b mb ON (ma._id = mb.model_a_id)
WHERE (mb.model_a_id = ?)
Hope it helps.
Till recent time i was using hibernate #Entity annotation to map to database tables.All the primary keys are annotated with #GeneratedValue(strategy = GenerationType.IDENTITY)
I got a scenario where i need to create new schema + migrate data from old schema into new schema.(with few column changes like drop, length and type)
After successful migration of data to new schema tables when i try to insert data using Application its throwing an exception
[ERROR] util.JDBCExceptionReporter DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, SQLERRMC=1; _NewSchema_._TableName_ , DRIVER=3.51.90
I believe that application is trying to insert rows again with Primary key value starting from 1 because same application is working fine with empty tables.
I want data rows to be inserted with its primary key value as highest value of existing rows primary key .
Any help will be thank full :)
Yes you can do that by altering the table. Alter the table and set starting index for identity column in DB2.
Suppose maximum rows for TBALE_A is 50 and name of identity column is TABLE_ID
ALTER TABLE TBALE_A ALTER COLUMN TABLE_ID
RESTART WITH 51
Your guess is correct, here is my solution, execute the following SQL to give the ID column a specified start position, then your application will work fine.
alter table TABLE_NAME alter column ID set GENERATED BY DEFAULT RESTART WITH 10000;
Hope to help you :)
In case of generation type , IDENTITY, you should look for identity column to be auto incemental.
#GeneratedValue(strategy = GenerationType.IDENTITY) required primary key column to be auto incremental.