Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question
0

Persisting Collections with Hibernate

Hello,

I am not sure whether this is a XAP question or a Hibernate question. I'll start by posting it here, and move on to Hibernate support if this is the wrong place.

I am using the external mirror service with the Hibernate external data source to persist data changes from the space to a DBMS and load data from the DBMS to the space on start-up. It is working well for simple space class objects, and for space class objects that contain @Embeddable objects. I am having trouble, though, when I try to persist a space class object that contains a collection of entity class instances using @OneToMany.

I have tried two Hibernate configurations, one that that creates a mapping table and one that doesn't.

Using the mapping table configuration, when I write my test object to the space, Hibernate creates a SQL batch that tries to insert the containing object and then the rows in the mapping table, but it doesn't include SQL statements to create the rows in the collection table first. The SQL batch fails with an integrity constraint violation: foreign key no parent.

When I use the configuration that doesn't create a mapping table, Hibernate persists the main class but doesn't attempt to persist the members of the collection to their table.

In both cases, the problem seems to be that Hibernate doesn't try to persist the contents of the collection.

In both cases the representation of my data in the space is correct. When I read the outer object from the space and inspect it, I can see that it contains the collection, which contains the members.

Also in both cases, Hibernate seems to do a perfect job creating the tables (three in one case, two in the other) as indicated by the annotations.

My questions are:

  1. Is what I am trying to do possible with the XAP – Hibernate integration?

  2. Is this a XAP question or a Hibernate question?

I am including the source code for my two test case classes (Menu, and MenuItem). They are set up for the no-mapping-table configuration. I think the comments show how I create the mapping table configuration.

Thanks for your help.

-Dan h4. Attachments

[MenuItem.java|/upfiles/1375970233470941.java]

[Menu.java|/upfiles/13759702337927741.java]

{quote}This thread was imported from the previous forum. For your reference, the original is [available here|http://forum.openspaces.org/thread.jspa?threadID=4017]{quote}

asked 2013-01-06 07:51:01 -0500

subuta gravatar image

updated 2013-08-08 09:52:00 -0500

jaissefsfex gravatar image
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

Dan, Its a XAP question. You should use DefaultHibernateExternalDataSource. You should use the @Fetch(FetchMode.SELECT) annotation You should not use lazy mode.

Shay

answered 2013-01-09 09:40:01 -0500

shay hassidim gravatar image
edit flag offensive delete link more

Comments

HI Shay,

It is good to hear from you.

I have been experimenting with @Fetch(value = FetchMode.SELECT) as you suggested and, so far, I haven't seen any change in behavior. Of course there are several possibilities for how to apply your suggestion, and perhaps I haven't stumbled upon the right approach. If you don't mind, let me ask you a couple of follow-up questions:

  1. Are you suggesting that I pursue the mapping table approach or the no mapping table approach?

  2. I guess the @Fetch(value = FetchMode.SELECT) annotation should be used in the containing class, not the contained class. Is that correct?

I have tried:

mapping table, annotations on both classes no mapping table, annotations on both classes no mapping table, annotation on containing class no mapping table, annotation on contained class

You probably saw that there is a reply from Neilson Ramalho indicating that he experienced the same problem and suggesting a work-around. I have asked him for more information.

Thanks for your help.

-Dan

subuta gravatar imagesubuta ( 2013-01-09 21:39:35 -0500 )edit
0

I had the same problem a few weeks ago. I fixed that by persisting the collections myself. Considering that hibernate won't persist anything in any of the approaches (mapping table and the other one), I had to persist the whole collection before saving the parent object. This way you won't have that foreign key constraint. I'm not sure if this is the right solution, but I could not make it work by other means. I will try to find my old code here to see if there is any other relevant information for you. I hope that helps. Neilson

answered 2013-01-08 16:38:02 -0500

edit flag offensive delete link more

Comments

Hi Neilson,

Thanks very much for the reply. I have a couple of questions:

  1. Were you working with the XAP - Hibernate integration, or are you talking about pure Hibernate without XAP?

  2. If you were working with XAP, how did you persist the collection yourself? Aside from the annotation I don't have any Hibernate references in my code, so I am not sure how I would persist the collection myself. Maybe by first writing the collection objects to the space so they will be persisted, then writing the containing object to the space?

-Dan

subuta gravatar imagesubuta ( 2013-01-09 21:12:56 -0500 )edit

Hi Nelson,

After about trying about 18,000 :) configurations, I STILL don't have this working correctly, but I have discovered the secret to getting the objects in the collection to persist. It is:

@OneToMany(cascade = CascadeType.ALL)

Happy to trade additional notes on this if you like.

-Dan

subuta gravatar imagesubuta ( 2013-01-13 06:18:51 -0500 )edit

Hi Dan, In my case, using cascade all was not an option. But that should work depending on your model. Thank you for your comments. I persisted the collection by saving it directly into the space. I was using the mirror approach. In my case, I had a User-Role-Permission module, so the sequence was: 1 - save the permissions 2 - save the roles 3 - save the RolePermission object. This object will contain references to the recent saved roles and permissions. In this case, when the mirror persists the RolePermission in the space, the other objects (roles and permissions) will be already there. I decided to go with this approach because I could not make it work using the default ManyToMany annotation. The problem is that you would have to persist each element of your collection individually and, since you are doing that, you should not use the cascade all. There must be other more elegant ways to do that, but this was the best I could do considering my time restrictions.

neilsoncarlos gravatar imageneilsoncarlos ( 2013-01-16 10:23:00 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-01-06 07:51:01 -0500

Seen: 149 times

Last updated: Jan 09 '13