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;
Related
I use ICsvBeanReader to read CSV files.
So what is the best workaround to update rows read from CSV file in the database without creating a new record if its id is new?
I can't use
saveAll(Iterable)
because it will create a new record if it's id not found.
Example
private Long id;
private int quantity;
private String name;
CSV File
id,quantity,name
1,30,name1
2,30,name2
20,30,name3
...
Note that:- row 20 is not found in the table so ignore it in an update.
JPA doesn't have an API to update an entity only if it exists, you can do a merge() which will update if the entity exists but it will also create a new row if it doesn't already exist.
Hibernate has an update() method which will update a row if it exists, but fail if the row doesn't exist.
I overcame this problem with a simple workaround.
Fetch all table ids and check if CSV row id found in the table ids list so add this row to the new list else ignore this row.
In final send the new list to saveall(iterable) method.
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.
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.