Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One class whose instances are contained in multiple other classes.

 
Christopher Rygaard
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

Can somebody point me to an example showing how I can have instances of one class contained in different kinds of container classes in Hibernate? I think my question is "What annotation do I put onto the instance of the contained class so that it can be contained by different classes of containers?".

My end users will be entering different kinds of engineering information, and I want to attach some explanation to each chunk of data (the various kinds of data chunks are represented by different classes). I have created an "ExplanatoryNotes" class to handle the explanations. I want to attach ExplanatoryNotes instances to the various data chunks, in different classes.

If somebody could point me to an example of how to do this, or if somebody could tell me how to get my example working, I would be most grateful.

P.S. I'm an experienced Java programmer, and I have some SQL experience, also. However, I am a rank beginner with Hibernate. I have built several Hibernate examples, including various kinds of relations (including a full-on tree), so I have some understanding of some of the basic concepts in Hibernate. Having said that, it is entirely possible that I am demonstrating a misunderstanding of Hibernate concepts: If so, please set me straight.

Thanks ahead of time for any input!

This is a distillation of my ExplanatoryNotes class:

This is the first (simplified) example of how I want to use ExplanatoryNotes:

And this is a second (simplified) example of how I want to use ExplanatoryNotes:

Finally, this is a (simplified) example of how I would use the data:
 
Christopher Rygaard
Ranch Hand
Posts: 42
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was perusing this forum and discovered the problem with embedded object in hibernate thread. It answered my question. The Hibernate concept that I was missing was "embeddable".

The fix was:

(1) In ExplanatoryNotes.java, the class should not be annotated as
@javax.persistence.Entity

(2) In ExplanatoryNotes.java, the class should be annotated as
@javax.persistence.Embeddable

(3) In ExplanatoryNotes.java, drop the field _id, along with all of its annotations.

(4) In ComplicatedData1.java and in ComplicatedData2.java, the _explanatoryNotes instance variable should be annotated with
@javax.persistence.Embedded

In the hopes that my experience can be useful for other greenhorns, here are the corrected classes:








 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher,
Thanks for posting the answer. Have a cow for that.

I intended to read this thread yesterday. But it was long and I kept getting called off to do other things.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic