简体   繁体   中英

hibernate Association Table with extra column

I'm currently working with hibernate and oracle SQL Developer and I have a problem. Indeed, I have 2 tables Employee and Building with a many-to-many association between them. So I have also created an association table in SQL Developer called Access which contains foreign key from Employee and Building (which are the primary key of the Access table) and an extra column called Times (Represents how many times an employee has been into this building). But I have a problem doing my access.hbm.xml because I don't know how to do it because it contains an extra column, and also what to put into my employee.hbm.xml and building.hbm.xml.

Currently, what I have into access.hbm.xml is a composite ID

<composite-id>
        <key-many-to-one name="ID_EMP"
            class="client.dataobj.hibernate.TestEmployee">
            <column name="ID_EMP_ACCESS" />
        </key-many-to-one>
        <key-many-to-one name="ID_BUIL"
            class="client.dataobj.hibernate.TestBuilding">
            <column name="ID_BUIL_ACCESS" />
        </key-many-to-one>
    </composite-id>

and nothing into employee or building related to this table access or to the other table. The problem, is that I have to do a criteria to get the employee name, the building name and how many times he has been there, but It doesn't work and I don't know why. Can someone help me please ?

You should use 2 one-to-many relationships in this case.

However, you are denormalizing your database for no apparent reason. Why not simply count the number of accesses when you need to?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM