is there a way that i get back the number of affected rows by using:
getHibernateTemplate().save(bean);
what is the point using save if you dont know what happened in the end.
Or, if i insert a new row, i want to get the new PK AI that was created....
Any idea?
Thanls
is there a way that i get back the number of affected rows by using:
Not for the "save" operation, which is quite understandable. But if you are using a Query, the executeUpdate method returns the number of affected records.
what is the point using save if you dont know what happened in the end.
Well, if there's no exception, you can assume everything went as expected.
Or, if i insert a new row, i want to get the new PK AI that was created....
Hibernate updates the persisted bean with the generated PK. So, if your bean has a long id, set with auto-increment, Hibernate will populate this field once it gets persisted.
Related
I'm using spring-batch jobs to persist content of a large csv file to a database.
JpaItemWriter is used for persistence, which is fine so far.
But now I'd like first to check if an entity already exists in the database (by id - the id field in csv and in database are equal), and in case just update the entity instead.
How could this be done?
When I needed to do this, the best I came up with was having my custom FieldSetMapper (used by the FlatFileItemReader) load the item from the database (or create a new instance of it doesn't exist) and then setting the properties based on the input. Since JpaItemWriter uses .merge, it will write the entity by updating if it was loaded from the database and insert if it was a new entity.
I also needed to have it run with a batch size of 1, to ensure that if there were duplicates in my input (which I did have), it would actually go one row at a time and insert or update for each one and not try to insert them all at once causing key problems.
As you might imagine, all this worked a lot slower than I would have liked. It queries the database for each and every row, and then does the corresponding update or insert. But as for my case it was for a monthly overnight batch process, it was good enough for our needs, even if it took many hours to run.
I have a question.
So I have a add functionality where the user can add cars to the database. How do I do a check whilst adding the car, so that if the car does exist-the data is overwritten, instead of an error messaging like 'Duplicate error' appearing?
So I have...
INSERT INTO Cars VALUES (1, "AUDI R8", 10);
How do I do it so that if a user inputs (1, "BMW X5, 15), it overwrites the current data?
How would I have an INSERT INTO and UPDATE STATEMENT at the same time? Also how do I make use of transactions here?
Many thanks
MySQL has a REPLACE statement for this kind of cases:
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted [...]REPLACE is a MySQL extension to the SQL standard. It either inserts, or deletes and inserts.
Check the reference for the REPLACE statement.
Well, the best solution for this problem are the TRIGGERS.
Triggers allow you to separate the application layer from the the database.
In your case you need to launch an error, so this is a trigger called "passive", that reacts automatically only if the condition is violated.
Take a look a this: https://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html or various related docs.
Maybe is not the best solution, but u can do a SELECT first, and after do an UPDATE or a simply INSERT, depends of the case.
I am using spring-hibernate-postgres. I have a table say 'some_entity'. It already contains some records.
I have a program that tries to create new SomeEntity object. I am populating that object with appropriate properties, and afterwards I am calling persist() method on it.
In the log, I see that hibernate is trying to get nextVal() from the table sequences. But, nextval that my postgres returns is same as id of 2nd row of some_entity table. So, my hibernate tries to create row with that id. And hence my persist() fails with hibernate constraint violation exception.
May be I am not phrasing the question correctly. I hope someone has already encountered this problem and has resolution for it.
Thanks
I had this problem. I solved it through execution of sql, that updates sequence at application launch
ALTER SEQUENCE names_id_seq RESTART WITH currentId;
,where currentId I get from
SELECT currval('names_id_seq');
My Use Case:
my Hibernate configuration using auto increment generator for insert and I don't want to change it.
A user deleted a object with ID:10 and I saved this deleted object to somewhere.
later on, user decide to restore this deleted object back with the same ID:10.
since this object with ID:10 has been deleted from the database, How can I use Hibernate to insert it back to database while the hibernate configuration using auto increment generator(remember: I need keep the same ID for this object in database)?
Thanks,
Alex
I doubt Hibernate will let you do it if you don't change the generator. What about inserting it and then updating it with direct SQL and invalidating any hibernate caches?
I doubt Hibernate will not allow you to do this. However you can go and write sql queries to update Tables [If you have used Table Generator] to change the current index position that can be used but it will hell of complicated logic since you will always have to keep track on which index record is not present
i´m currently working on my first Java application based on a MySQL DB. I´m using EclipseLink2.0 and NetBeans, at the time i am facing a behaviour i cannot explain, maybe someone has stumbled over this problem in the past and can help me with this. Here goes:
Table 1 has the PK of table 2 as Fk. Application-side, there is an UI where users can generate content for table 1. The value for the fk(Table2ID) is beeing chosen with a dropdown menu, which gets each values by reading the Collection of table2 rows. Now, when i try to change the value for the fk to another (already existing) value, instead of doing just that, a new row with a fresh ID is generated on table2, all other column values are cloned from the row i tried to change the Fk to. So, for example, when i try to set table1 rows 3,4 and 5 to table1.fkcolumn =6 (i.e Table2ID=6), the program instead clones the row with ID=6 3 times and sets each of the table1 columns to one of them.
Any help would be greatly appreciated .
The problem is you are changing the primary key of an entity. In EclipseLink, when you change the PK of an entity, you have a new entity. As such, Eclipselink inserts the new rows and leaves the old rows alone.
To get around this you have three choices:
1) Change the database. Primary keys really shouldn't be changed.
2) Set the application to execute an update query which changes the primary key values and requery them.
3) Delete the old rows and re-create with a new primary key.