Hi,
The argument is mixing type-level inheritance with instance level
parent-child relationship.
class A extends B {}
A a1 = new A();
B b1 = new B();
OOP does not say a1 and shares a a parent-child relationship.
But loosely speaking, one can say A is a "child class" of B.
Now if the following is the case:
class A extends B {
@ManyToOne(cascade=REMOVE)
B parent;
}
Then:
A a1 = new A();
B b1 = new B();
B b2 = new B();
a1.parent = b1;
em.remove(a1);
em.commit();
Will delete both a1 and b1 from the database. While b2 will remain.
It is possible within JPA, to delete a1 which w
Paul D. wrote:
>
> Thank you for your explanation.
> Well, from OOP perspective my case is perfectly valid since MyProduct
> inherits most of UserProduct properties, but can be deleted, unlike
> UserProduct which just changes it's status. My app dictates these rules.
> Looks like this is another case when JPA dictates mapping rules rather
> than being a flexible tool which adapts to application needs.
> Disappointing. I think JPA needs cascading attribute for inheritance cases
> so that developer can choose what to delete and what not.
> And BTW why it throws optimistic violation exception instead of just
> gracefully deleting everything, parent and child? It's all going in one
> transaction.
>
>
>
> Simone Gianni wrote:
>>
>> Hi Paul,
>> I think you are misusing the JOINED feature. When you map a hierarchy
>> using JOINED you are simply telling JPA that it should persist
>> additional fields in different tables. This does not mean that they are
>> separate entities or that it's possible to transform one entity to
>> another class. Even if JPA is saving on more than one table, that is a
>> single and unique entity, and cannot be considered like two different
>> entities (a parent and a child one), so persist or delete one and not
>> the other.
>>
>> The only way I can think of is creating a new UserProduct entity,
>> transfer data from the MyProduct instance to the new one, delete the
>> MyProduct instance, then persist the new one preserving the id, but this
>> could cause a number of problems if other bean are referencing this
>> entity assuming it is a MyProduct.
>>
>> Probably, if you need to use MyProduct and UserProduct as separate
>> entity, you should not have one extend the other, but have two distinct
>> entities connected one to the other using a @OneToOne. For example :
>>
>> public class UserProduct {
>> @OneToOne
>> private MyOptionalData mydata;
>>
>> }
>>
>> public class MyOptionalData {
>> @OneToOne
>> private UserProduct product;
>> }
>>
>> Hope it helps,
>> Simone
>>
>> [email protected] wrote:
>>> I have a class defined as
>>> @Entity
>>> @Table(name = "user_product")
>>> @Inheritance(strategy = InheritanceType.JOINED)
>>> public class UserProduct implements Serializable {
>>> private Integer productid;
>>> }
>>>
>>> then I have a child class defined as
>>> @Entity
>>> @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "productid")
>>> public class MyProduct extends UserProduct implements Serializable {
>>> private Integer id;
>>> }
>>>
>>> How do I delete MyProduct without deleting its parent? The parent
>>> needs to be just updated, but not deleted.
>>> When I try to set a value and merge UserProduct, and then delete
>>> MyProduct I get "An optimistic lock violation was detected when
>>> flushing object instance...".
>>>
>>> Help is very much appreciated.
>>
>>
>> --
>> Simone Gianni CEO Semeru s.r.l. Apache Committer
>> http://www.simonegianni.it/
>>
>>
>>
>
>
--
View this message in context:
http://n2.nabble.com/How-to-delete-polymorphic-entity--tp2211525p2237249.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.