facebook

Hibernate Many-toMany

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #248374 Reply

    Eugen31
    Member

    Hi all again!

    I am very new in hibernate and java, and i would appreciate some help or example that someone have.

    I have a simple many-to-many association! Like Person and Equipment and we create a Person_Equipment table with two PKs like PersonID and EquipmentID.

    How can i create the mapping file for this situation?
    And the java class? How am i create this?!?!?

    Thanks,
    Eugen Lechner

    #248398 Reply

    Haris Peco
    Member

    Eugene,

    Select your tables in Db explorer, right clikc and call ‘Create Hibernate mapping’ – MyEclipse will make mapping and POJO for you
    If you have problem further, send us your database type, version, Eclipse and MyEclipse version and your schema script (DDL with PK and FK)

    Best

    #248417 Reply

    Eugen31
    Member

    Hi,
    I use PostgreSQL 8.1, Eclipse SDK 3.1.2 and MyEclipse 4.1.1.
    DDL:
    CREATE TABLE person (
    id int8 CONSTRAINT PK_person_id PRIMARY KEY,
    firstname varchar(50),
    surname varchar(50)
    )
    WITHOUT OIDS

    CREATE TABLE equipment (
    id int8 CONSTRAINT PK_equipment_id PRIMARY KEY,
    title varchar(50),
    description varchar(500)
    )
    WITHOUT OIDS

    CREATE TABLE person_equipment (
    person_id int8,
    equipment_id int8,
    CONSTRAINT FK_person_id FOREIGN KEY (person_id) REFERENCES person (id),
    CONSTRAINT FK_equipment_id FOREIGN KEY (equipment_id) REFERENCES equipment (id)
    )
    WITHOUT OIDS

    The MyEclipse Hibernate tool creates 3 mapping files for person, equipment and person_equipment and 3 POJO-Classes. I need only two POJO classes with association HashSets between person and equipment.

    #248423 Reply

    Haris Peco
    Member

    Eugen,

    I understand, but it’s hard that MyEclipse decide when you want many-to-many and we don’t support this.
    If you want many-to-many mapping you have to change it manual
    For your example you can remove PersonEquipment* (POJO and mapping) and change Person.hbm.xml to :

    
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse - Hibernate Tools
    -->
    <hibernate-mapping>
        <class name="Person" table="person" schema="public">
            <id name="id" type="long">
                <column name="id" />
                <generator class="increment" />
            </id>
            <property name="firstname" type="string">
                <column name="firstname" length="50" />
            </property>
            <property name="surname" type="string">
                <column name="surname" length="50" />
            </property>
            <set name="personEquipments" table="person_equipment" >
                <key>
                    <column name="person_id" />
                </key>
                <many-to-many class="Equipment" column="equipment_id" />
            </set>
        </class>
    </hibernate-mapping>
    

    I don’t sure if we have add features for many-to-many mappings

    Best

    #248425 Reply

    Eugen31
    Member

    Thanks.

    Can MyEclipse make POJO from mapping file with many-to-many association?

    Eugene

    #248426 Reply

    Eugen31
    Member

    @support-snpe wrote:

    Eugen,

    I don’t sure if we have add features for many-to-many mappings

    Best

    I think the missing support of many-to-many association would be the first reason, why one did not become to use this tools for development in a company.

    #248433 Reply

    Haris Peco
    Member

    Can MyEclipse make POJO from mapping file with many-to-many association?

    yes.version 4.1.1 can do it

    I think the missing support of many-to-many association would be the first reason, why one did not become to use this tools for development in a company.

    ok.I think that it can be cool features, but what you think how MyEclipse can decide what is many-to-many
    – tables with composite PK and all columns in PK are FK ? or how ?

    Thanks

    #260834 Reply

    gavin9
    Member

    @support-snpe wrote:

    ok.I think that it can be cool features, but what you think how MyEclipse can decide what is many-to-many
    – tables with composite PK and all columns in PK are FK ? or how ?

    Thanks

    My suggestion is to make an option in the dialog, simple check box “Weak Entity” would be good where the IDE can determine the relationships from FK or even let the user map FK to tables via drop down boxes.

    Regards

    #260894 Reply

    +1 i have the same problem on my projects and i have to make the right changes manualy 🙂

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: Hibernate Many-toMany

You must be logged in to post in the forum log in