1

Am I misunderstanding a basic concept of Restful web services? I have an Android app that I am trying to use a Restful PUT. Two Mysql tables Country and StateProvince with countryId a foreign key on StateProvince table.

If I try to do a PUT to StateProvince using the following

<StateProvince><stateName>Victoria</stateName><countryId>1</countryId></StateProvince>

I get the error below. Am I misunderstanding a basic concept regarding foreign keys and Rest?

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DatabaseExcepti on
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException: Column 'country_id' cannot be null
Error Code: 1048
Call: INSERT INTO state_province (state_id, state_name, country_id) VALUES (?, ?, ?)
bind => [3 parameters bound]
Query: InsertObjectQuery(com.pezoot.models.StateProvince[ stateId=2 ])
Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99

2 Answers2

1

Short Answer: country_id is null, so this looks like a database/persistence issue. You probably didn't set the Country for the StateProvince (or add the StateProvince to the Country - haven't seen your code so I don't know how you're mapping things).

Long Answer:

Why is there an database identifier coming in as part of your HTTP request?

You need to start thinking in terms of URIs and resources - your StateProvince representation should have some kind of link that relates to a country at a particular URI (e.g. <link rel="country" href="/country/1" /> and in your resource class that handles the PUT verb, you need to be able to conver that URI in to a domain object, an entity (as it seems you're using EclipseLink) which you can use some setter method or something on to establish the database relation. The REST relationship and the database relationship are fundamentally different.

It takes practice and careful thinking to handle what seems like a simple concept (HTTP verbage) against your persistence unit. Something that seems straightforward like PUT has nontrivial processing required in order to make it work as REST would expect.

It is tempting to use database identifiers in URIs because it is easy (especially if you use subresources that just happen to magically know who their parent is: e.g. country/1/stateprovince/2) but you have to step back and ask yourself, is it country/1 or is it country/usa - you also have to ask yourself, is the country and state/province really an entity? or is it just a value object? Do you really intend to PUT a State/Province in its entirety?

Doug Moscrop
  • 4,479
  • 3
  • 26
  • 46
0

Thanks guys.

Once again (unsurprisingly) it appears to be a syntax error. When I used the following:

<stateProvince> 
       <countryId> 
           <countryId>1</countryId> 
      </countryId> 
       <stateName>New South Wales</stateName> </stateProvince>

Hey presto it works. I had failed to embed the countryId as shown above previously (see old code below)

<stateProvince> 
       <countryId>1</countryId> 
       <stateName>New South Wales</stateName> </stateProvince>

And thanks Doug - your response is the sort of insight I am seeking. I dont believe I have quite wrapped my head around the use of links as you describe - but I will investigate further now