tags:

views:

117

answers:

2

I have a Parent Table A and child tables B,C with many to one relations.

Lets say I have data like, for primary key 1 in parent table A I have 3 rows in child table B and 4 rows in child table C.

Now if I want to delete the rows of the child table during an update of the parent table(that means Now, I want to update the table only with one row in each child tables and delete the other rows in them) then how to handle this scenario in hibernate?

Update

Firstly, sorry that my question was unclear. Here are more details of it.

I have a parent table Policy

CREATE TABLE "DEV2"."POLICY" ( "POLICY_OID" NUMBER(38,0) NOT NULL ENABLE, "CREATED_DATE" TIMESTAMP (0) NOT NULL ENABLE, "EFFECTIVE_DATE" TIMESTAMP (0), "UPDATED_DATE" TIMESTAMP (0), "STATUS" VARCHAR2(32 BYTE), CONSTRAINT "PK128" PRIMARY KEY ("POLICY_OID") , CONSTRAINT "REFPOLICY_CLASS290" FOREIGN KEY ("POLICY_CLASS") REFERENCES "DEV2"."POLICY_CLASS" ("POLICY_CLASS_REF") ENABLE )

and @OneToMany relation to child table POLICY_RELATIONSHIP

CREATE TABLE "DEV2"."POLICY_RELATIONSHIP" ( "POLICY_RELATIONSHIP_OID" NUMBER(38,0) NOT NULL ENABLE, "POLICY_ACTOR" NUMBER(38,0) NOT NULL ENABLE, "POLICY_ACTOR_TYPE" VARCHAR2(32 BYTE) NOT NULL ENABLE, "POLICY_OID" NUMBER(38,0) NOT NULL ENABLE, CONSTRAINT "PK156" PRIMARY KEY ("POLICY_RELATIONSHIP_OID") , CONSTRAINT "REFPOLICY338" FOREIGN KEY ("POLICY_OID") REFERENCES "DEV2"."POLICY" ("POLICY_OID") ENABLE)

For Example, I have the sample data something like this.

POLICY_OID CREATED_DATE EFFECTIVE_DATE STATUS UPDATED_DATE
1234 06/14/2020 06/14/2010 active 06/14/2010

POLICY_RELATIONSHIP_OID POLICY_ACTOR POLICY_ACTOR_TYPE POLICY_OID
98765 John Primary User 1234

98766 Bill Secondary User 1234

98767 Mary Intermediate User 1234

If I try to persist the Policy Object 1234 and UPDATED_DATE changed to 06/15/2010 and the PolicyRelationship has data like below

POLICY_RELATIONSHIP_OID POLICY_ACTOR POLICY_ACTOR_TYPE POLICY_OID

null George Primary User 1234

Is there any possible way with hibernate, to delete the three rows which already exist in the PolicyRelation table and insert an new row with new Data.

Please let me know if my problem is still unclear.

My Environment:

Java 1.6, Hibernate 3.5, JBoss.

A: 

Being from the _N_Hibernate side of things, this may not apply, but can't you set cascade options (In NHibernate it would be cascade="all, delete-orphan")? Then just make sure you set the parent of each child to null before saving the parent.

Kirk Clawson
A: 

Do this in the java code, and if your mappings are correct everything will be ok. If you're using some sort of cascade 'save-update', or 'all' your code would be something like this:

policy.getPolicyRelationships().clear();
PolicyRelationship pr = new PolicyRelationship();
...
pr.setParent(policy);
policy.getPolicyRelationships().add(pr);
dao.save(policy);

Or overwrite the existing set of children with the values you want.

policy.getPolicyRelationships().clear();
Set<PolicyRelationship> prSet = new TreeSet<PolicyRelationship>();
PolicyRelationship pr = new PolicyRelationship();
prSet.add(pr);
...
pr.setParent(policy);
policy.setPolicyRelationships(prSet);
dao.save(policy);

Alternately you could set the relationship to use cascade="all-delete-orphan" which means hibernate should remove those children once they lose their reference to a parent entity. This is relevant in case you delete policy, then the policy relationships will be deleted from the DB as well.

zmf
If I do the second approach the foreign key in PolicyRelationship is NOT NULL so, this would lead to DataIntegrityViolation Exception. Isn't it?
Harsha
Yeah you're right. The more I think about it, that second example was unclear
zmf
Can you tell me what was unclear.So that I will know what more information I should provide.
Harsha
I tried the first approach and it did not work as I expected.
Harsha