1

I am new to hibernate and i want to map a table having only foreign keys using xml file. I have 3 tables 1.UserInfo(for user details with a primary key) 2.AuthorInfo(containing author details with a primary key) 3.AuthorFollow(to map which user is following which author). The AuthorFollow table contains only 2 columns UserId and AuthorId both are having relationships(foreign keys) with the 1st 2 tables respectively. I haven't given any index on these columns.

I have written below code in AuthorFollow.hbm.xml

But, its not working properly.

    <?xml version='1.0' encoding='utf-8'?>

    <composite-id>
        <key-many-to-one name="userId" column="user_id" class="com.pojo.hibernate.UserInfo" />
        <key-many-to-one name="authorId" column="author_id" class="com.pojo.hibernate.AuthorInfo" />
    </composite-id>

</class>

(i have used mysql.)

And, is the table structure proper?

Ashish Tanna
  • 645
  • 1
  • 10
  • 25
  • 1
    You shouldn't map this table at all. There should be a many-to-many association between Author and User. And please, XML mapping was cool when we all used JDK 1.4, 8 years ago. Use annotations to map your entities. – JB Nizet Mar 03 '13 at 15:08
  • thanks for the answer.& of course it worked. but, i have another question. If that mapping table contains a 3rd column then what should i do? – Ashish Tanna Mar 05 '13 at 18:55
  • 1
    Then you should map it, and transform the ManyToMany into two OneToMany associations. – JB Nizet Mar 06 '13 at 08:09

0 Answers0