1

I created a @OneToMany relationship with Entities. For instance, Member and Address. These two are joined by Mem_Addr table.

When I deploy the application, the log shows:

 EntityBinder  I org.hibernate.cfg.annotations.EntityBinder bindTable Bind entity Address on table ADDRESS

But the table doesn't exist the in the database.. Not sure how this happens?!?!

Secondly, the Hibernate query (from the Log) to pull members, which is expected to show Address Query as well. But the query does not have Address / Mem_addr tables included. Its just pulling Members alone, ignoring address, despite @OneToMany annotation.

Any ideas? Does Hibernate intelligently recognizes non-existance of table and does not include that table in the query?

Edit Updated for clarity.

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

2 Answers2

1

I haven't used Hibernate in a while, but as I recall you have to pretty much tell Hibernate exactly how you want it to operate. In other words, in your annotations, you need to specify exactly how you want your records to be loaded. You might want to read up on the OneToMany annotation.

Off the top of my head you'll need:

@Entity
public class Member {
    private Set<Address> addresses = new HashSet<Address>();

    @Id
    ...

    @OneToMany
    @JoinTable(
            name="MEM_ADDR",
            joinColumns = @JoinColumn( name="MEMBER_ID"),
            inverseJoinColumns = @JoinColumn( name="ADDRESS_ID")
    )
    public Set<Part> getAddresses() { return addresses; }
    ...
}

@Entity
public class Address {
    ...
}

EDIT: Addressing eager vs lazy loading

FYI, you can force eager loading in your @OneToMany annotation as the default is Lazy

@OneToMany(fetch=FetchType.EAGER)

Hope that helps.

mattforni
  • 855
  • 5
  • 11
  • So first it sounds like you haven't created the Mem_Addr table, which you need to do. Hibernate does not create tables for you, it just reads/writes data from the database. Once you've created the table you need to tell Hibernate how to make the OneToMany mapping by adding a @JoinTable annotation. The following post should help: http://stackoverflow.com/questions/13047483/hibernate-one-to-many-using-a-join-table-and-hibernate-annotations – mattforni Sep 25 '14 at 18:14
  • But my other problem is Address does not have Primary. Primary key is in Mem_addr table. So Members (id PK)-->Mem_Addr(a_id pk)-->Address (a_id Fk). How do I create Mapping here? – Kevin Rave Sep 25 '14 at 18:39
  • Each entity table (not join table) should really have a primary key ... especially if you're going to use it in a join table. After all, that's really all a join table is, an association between two primary keys of two different entities. I would recommend adding a PK to Address. – mattforni Sep 25 '14 at 18:50
  • I wish I can change it. But I just have to deal with it :-(. Any work around? Here is the [detailed question](http://stackoverflow.com/questions/26046275/hibernate-mapping-joining-two-tables-with-an-association-table-but-with-a-tw) – Kevin Rave Sep 25 '14 at 19:11
0

Okay. This is what I figured out. Though during deploymnet, it says Entity bound to the table, I guess it did not.

But since the dependencies are Lazy loaded, it generates the query with its dependencies only when the dependencies are used/requested. And thats when it throws the error that the table does not exist.

I thought it throws the error during bind time itself. But it does not, at least in my case.

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173