This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Why there is no delete by id in JPA? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Why there is no delete by id in JPA?" Watch "Why there is no delete by id in JPA?" New topic
Author

Why there is no delete by id in JPA?

Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
I curious why there is no delete by id in JPA?
So if I know id and want to delete I need to find first then delete, it's unnecessary to me.

Does anybody know the reason?


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Kengkaj, Kengkaj, Kengkaj! Stop thinking like an SQL developer!

JPA is based on Objects, like Java. We don't just throw around numeric ids and column names. We are Java people!

The delete method takes an object. You just pass in an object. Which object do you want to delete? Just pass it in. Does it have a composite key, a sequence generated key, a numeric key or text key? [b]We don't care, we are Java developers.

Of course, the object we pass in will have an id in it. It will also be of a certain type of class. So, JPA, given the class, and the object that will obviously have an id embedded somwhere inside of it, can delete the underlying data, be it from one table, or spread across multiple tables, as those uncouth database designers are loath to do.

To be honest, if you just want to delete a row, given an id, you can do something like this:



That basically just passes an empty object with an id to the EntityManager or Hibernate Session for an immediate delete.

Think objects, my dear boy, objects!

-Cameron McKenzie



Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Cameron Wallace McKenzie wrote:
To be honest, if you just want to delete a row, given an id, you can do something like this:



That basically just passes an empty object with an id to the EntityManager or Hibernate Session for an immediate delete.

-Cameron McKenzie

I'm no expert in ORM, but I don't think it will work in JPA, EntityManager.remove throws IllegalArgumentException - if not an entity or if a detached entity.
http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html#remove(java.lang.Object)

Can Hibernate do that?

Anyway, I'm not convinced by the reason think objects, using your logic, JPA should not have find by id?
If we want to find an object which we know its id, we need to create an empty object, set id to it, and pass to a find method? Not make sense to me. And JPA doesn't do that, it has a find by id method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why there is no delete by id in JPA?