JPA DAO Update row without full row context

268 views Asked by At

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

1

There are 1 answers

4
shanehoban On BEST ANSWER

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.