• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hibernate embeddable container?  RSS feed

Christopher Rygaard
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I make an embeddable class a container for another class?

I have an embeddable class, and I want it to hold a java.util.Set of some other class (and some other stuff, also). When I make my greenhorn attempt to implement this, I get an AnnotationException saying that the embeddable class is an unknown entity. This occurs in the call to org.hibernate.cfg.Configuration.buildSessionFactory(). For full details, see the code below.

These are my greenhorn thoughts on what is happening: When Hibernate encounters an Embeddable class, Hibernate does not view it as a separate, distinct class. Instead, Hibernate simply maps the instance variables of the Embeddable class into the table columns of the class in which the Embeddable class is embedded. Because the elements of the Embeddable class are mapped to the table of a separate class, Hibernate no longer knows of the Embedded class. At this point, when Hibernate encounters my ManyToOne annotation in the containing class (lines 8 and 9 in Contained.java, below), it's annotating a class that Hibernate doesn't recognize.

Two questions:
  • Is my greenhorn analysis correct, and
  • Is there any way to make this work?

  • Here is my attempt at the Embeddable class:

    Here is the class I want to contain in the Embeddable class:

    Here is one example of how I try to use the embedded class:

    Here is another example of how I try to use the embedded class:

    And here is how I try to work the whole thing:

    The above code fails in HibernateExample.java, at line 34, with this error:

    Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.cummingsaerospace.sft.hibernateexamples.embeddedcontainer.Contained._embedded references an unknown entity: MyEmbedded
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1596)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1519)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at HibernateExample.<init>(HibernateExample.java:34)
    at HibernateExample.main(HibernateExample.java:8)
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!