0

Have a table with >70 columns.

I use about 8 of these columns with the .java object, including the id - not sure if that matters.

My Id column has the following:

@Column(name = "pseudonym", nullable = false)
@Basic(fetch = FetchType.EAGER)
@Id

My question is this:

If I want to update a row using xxxDAO.store(updatedRow) - do I need to specify anything to reference the row ID or am I missing something here?

I'm getting the following exception:

DEBUG: org.springframework.web.servlet.DispatcherServlet - Could not complete request
javax.persistence.PersistenceException: org.hibernate.exception.DataException: Could not execute JDBC batch update
.. ~(adding this in here)~ not a valid month

Hopefully I have explained this well enough. Let me know if I haven't.

Thanks in advance

Some pseudo code for reference:

item = dao.getItemById(123);
item.setUpdateTime(theTime);
dao.store(item);

Store: return getEntityManager().merge(itemToStore);


To Clarify

I was getting the exception above not a valid month. This was because I was trying to update an Oracle table that had a column defined as a timestamp. I was trying to pass a String value (even though the String was essentially structured identically).

In an attempt to debug this, my question was if I could update a row via the JPA DAO without having e.g. all 70+ columns defined in my Java object. This turned out to be possible and the exception was not related to not having defined columns. The exception was simply because the timestamp I was trying to update didn't have the correct structure. I changed the Strings to Date objects, and they updated

Community
  • 1
  • 1
shanehoban
  • 870
  • 1
  • 9
  • 30
  • You don't need `nullable=false` and `FetchType.EAGER` for an `@ID` attribute. And `TABLE` is a keyword, if your ID column is really named that way, you will have to escape it: `@COLUMN(name="\"TABLE\"")` – Tobias Liefke Jun 25 '15 at 14:11
  • @TobiasLiefke - Sorry for confusion, that's a pseudonym for the table. Thanks for the other pointers – shanehoban Jun 26 '15 at 08:04
  • When you ask about an exception, always post the complete exception stck trace, and the code causing this exception. – JB Nizet Jun 26 '15 at 08:08
  • Yes I understand that JB Nizet but I am not in the position to do so; apologies. – shanehoban Jun 26 '15 at 08:21

1 Answers1

0

My question was this:

If I want to update a row using xxxDAO.store(updatedRow) - do I need to specify anything to reference the row ID or am I missing something here?

Answer

No - It is managed automatically, you don't need to specify an identifier, and you don't need to create all references for all the columns in the table either in your java object.

item = dao.getItemById(123); // get item from dao in the database
item.setUpdateTime(theTime); // set/or change a value
dao.store(item); // contains merge(), and subsequently flush()

item contains the reference to what row will be updated.


Additionally, when dealing with timestamps in your sql table, ensure that all are actual Date objects and not Strings.

When pulling the date as a String for read only is fine. But if you want to update this row in the future via a merge, the String will not work. The value for the date will be re-passed again (as a String), and cause an exception to be thrown (e.g. not a valid month). Ensure your dates/timestamps are stored as Date objects as opposed to Strings and perhaps toString them on your UI.

shanehoban
  • 870
  • 1
  • 9
  • 30
  • I don't understand, what the _Additionally_ section has to do with the original question? You should extend the model and exception in your post first, otherwise someone with the same problem won't find your solution. – Tobias Liefke Jun 28 '15 at 19:01
  • @TobiasLiefke - This was something I ran into when trying to solve the original issue - and perhaps if Google indexes it; and allows even one person to find/see the information - it's worth it – shanehoban Jun 29 '15 at 09:05
  • But you still don't explain, why you did run into the exception - I thought it would be the date problem, thats why I asked to add that information to your original question. – Tobias Liefke Jun 29 '15 at 12:07
  • Oh apologies, yes the exception was caused by the date problem. I will add those details now – shanehoban Jun 29 '15 at 12:58