0

I'm not sure my syntax is correct.

My controller

@RequestMapping(value = "/bakedGoods/{id}", method = RequestMethod.DELETE, produces = "application/json")
    public @ResponseBody ResponseEntity<Object> deleteABakedGood(@PathVariable long id){
        bakedGoodsService.deleteABakedGoodInDB(id);

My Service

public void deleteABakedGoodInDB(long id) {
        bakedGoodsDAO.deletBakedGood(id);

My DAO

@Transactional
    public void deletBakedGood(long id) {
        em.remove(id);
        em.flush();

It says the long id isn't defined but the it is defined in the entity as id.

Kenandrae
  • 15
  • 10
  • 1
    What says what? Post the exact and complete error message you get. Tell us when and how you get it. Also, a DELETE request is not supposed to return any response body. – JB Nizet May 12 '15 at 18:07
  • SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/finalproject-wheatt-api] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Unknown entity: java.lang.Long] with root cause org.hibernate.MappingException: Unknown entity: java.lang.Long – Kenandrae May 12 '15 at 18:12
  • I get the error whenever I try to use Advanced rest client to delete a Item in the baked goods table. When the process is complete i want it to return a Httpstatus ok. – Kenandrae May 12 '15 at 18:15

1 Answers1

0

OK. So the exception is thrown by

em.remove(id);

And it says:

IllegalArgumentException: Unknown entity: java.lang.Long

And the javadoc of EntityManager.remove() says:

Remove the entity instance.

Parameters: entity - entity instance

So, remove() doesn't expect the ID of an entity. It expects an entity instance. BTW, with only an ID as argument, how could the entity manager know which type of entity it must delete?

You thus need something like

 BakedGood entity = em.getReference(BakedGood.class, id);
 em.remove(entity);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hi JB, can you please answer to my post -- http://stackoverflow.com/questions/30357979/when-to-use-request-session-application-scope-in-spring – learner May 21 '15 at 17:14