收录日期:2020/11/26 22:32:01 时间:2010-09-07 20:25:25 标签:hibernate

I have two tables in parent child relation. Parent can have multiple child records. Primay key of parent table is generated by hibernate, this generated primary key of parent table is foreign key in child table. There is cascade=all set in parent hbm. So after parent key is generated and parent record is saved, child record should get saved with same generated key value. Only way I know to do this is with "foreign" generator but for one-many relation with parent-child I can't have parent-key as primary key in child table. it results in UniqueObjectException. Anyone know how can I save generated parent key value in child table in this scenario?

So after parent key is generated and parent record is saved, child record should get saved with same generated key value.

This doesn't make any sense for a one-to-many relation, you can't use the PK of the parent as the PK of a child if you can have many children.

Only way I know to do this is with "foreign" generator but for one-many relation with parent-child I can't have parent-key as primary key in child table. it results in UniqueObjectException.

Yes, obviously. What result do you actually expect? What could a possible database design look like?

Anyone know how can I save generated parent key value in child table in this scenario?

Well, you actually get it (as foreign key). And I don't really understand what the real goal is. Could you maybe you explain what a possible database design would be, and what the real problem is so that we could think about a solution?