JPA-RS problem: java.lang.ClassCastException: java.util.ArrayList cannot be cast... [message #1007121] |
Mon, 04 February 2013 14:29 |
Daniel Szalay Messages: 15 Registered: February 2013 |
Junior Member |
|
|
Hi! I successfully set up a web application with EclipseLink 2.4.1 and JPA-RS on GlassFish 3.1.2.2. I can retrieve data through the GET method as described in the EclipseLink WIKI, however I have problems with the PUT operations.
Here is my entity class:
@Entity
public class UserTable implements Serializable {
@Id
@GeneratedValue
private long id;
@Column
private String userId;
@OneToMany(mappedBy="table", orphanRemoval=true, cascade=CascadeType.ALL)
private List<TimeRow> rows;
public UserTable() {
this.rows = new ArrayList<TimeRow>();
}
public UserTable(String userId, List<TimeRow> rows) {
this.userId = userId;
this.rows = rows;
}
}
I want to create a new UserTable entity with a PUT request using the URL "/persistence/unit/entity/UserTable", and the request body:
[
{
"userId": "testuser"
}
]
Here is the response:
Quote:The server encountered an internal error () that prevented it from fulfilling this request. javax.servlet.ServletException: java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.eclipse.persistence.internal.descriptors.PersistenceObject.
Am I doing something wrong? Maybe the request body should be syntactically different? I would expect that I don't have to specify an id (@GeneratedValue), neither the fields that are initialized in the constructor.
I've read in an article that: Quote:The last step is to set the Persistence Units to Local Resource instead of JTA Datasource, else it won't work.
I didn't find any evidence in the EclipseLink docs to prove this. My unit is defined to use 'JTA'; is this a problem?
[Updated on: Tue, 05 February 2013 12:18] Report message to a moderator
|
|
|
|
Re: JPA-RS problem: java.lang.ClassCastException: java.util.ArrayList cannot be cast... [message #1007537 is a reply to message #1007360] |
Wed, 06 February 2013 10:57 |
Daniel Szalay Messages: 15 Registered: February 2013 |
Junior Member |
|
|
Now I am using EclipseLink 3.4.2, and tried it with the following simple entity class without relationships:
@Entity
@NamedQueries({
@NamedQuery(name="Test.findAll",
query="SELECT t FROM Test t")
})
public class Test implements Serializable {
@Id
@GeneratedValue
private long id;
@Column
private String userId;
@Column
private List<String> names;
}
The ClassCastException occurs when I define the object wrapped inside [] brackets. I have tried to use the PUT method with the following request bodies:
{
"id": 1,
"userId": "TEST",
"names": ["one","two"]
}
The previous is executed successfully. I would expect that I don't have to specify the id field, since it is a @GeneratedValue, however for this request body:
{
"userId": "TEST",
"names": ["one","two"]
}
i get this response:
Quote:400 - Bad Request The request sent by the client was syntactically incorrect (Bad Request).
What am I doing wrong? Is this the normal behavior?
[Updated on: Wed, 06 February 2013 16:21] Report message to a moderator
|
|
|
Re: JPA-RS problem: java.lang.ClassCastException: java.util.ArrayList cannot be cast... [message #1007705 is a reply to message #1007537] |
Thu, 07 February 2013 08:21 |
Daniel Szalay Messages: 15 Registered: February 2013 |
Junior Member |
|
|
Okay, I've found the reason in EclipseLink/Release/2.4.0/JPA-RS/REST-API:
Quote: PUT is required to be idempotent. As a result, it will fail if called with an object that expects the server to provide an ID field. Typically this will occur if the metadata specifies a generated key and the field that contains that key is unpopulated.
Still, my question is: what is the best practice in this case? Providing a web service that returns an available id? This would lead straight to the TOCTOU problem, except if it is backed up by a bean that never gives out the same ID twice and has an ID pool for every entity type.
Also, I am confused that PUT is defined as PERSIST, and POST is defined as MERGE. So with POST I don't have to specify the ID? Couldn't really get POST working unfortunately.
[Updated on: Thu, 07 February 2013 13:11] Report message to a moderator
|
|
|
|
|
|
Re: JPA-RS problem: java.lang.ClassCastException: java.util.ArrayList cannot be cast... [message #1012854 is a reply to message #1011925] |
Fri, 22 February 2013 10:37 |
Daniel Szalay Messages: 15 Registered: February 2013 |
Junior Member |
|
|
Thanks for your response! I updated EclipseLink to 'eclipselink-2.5.0.v20130220-795a5ad' (nightly). Now, both POST and PUT result in this error message:
Quote:
HTTP Status 415 - Unsupported Media Type
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).
is set in header.
UPDATE
Okay, I changed the header to Content-Type: application/json and now it seems to work.
Now I would expect that I should use POST to insert an entity and use PUT to update an entity. Is that right? At the moment I can do both with POST, but PUT throws an internal server error (HTTP code 500):
Quote:
org.eclipse.persistence.exceptions.TransactionException
Exception Description: Error committing externally managed transaction
Where the request body is:
{
"id": 1,
"userId":"test",
"names":["one","two","three"]
}
Are there examples/tutorials out there which explain how to convert the retrieved data back to object instances? I tried to do so with Jackson, but it cannot consume the "_relationships" attribute.
[Updated on: Wed, 27 February 2013 15:58] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03948 seconds