Search code examples
javahibernatemany-to-many

How to switch an Hibernate many-to-many relationship from xml to annotation based configuration?


<set name="Units" table="UNIT" mutable="false">
        <cache usage="read-only"/>
        <key column="ID"></key>
        <many-to-many column="U_ID" class="com.org.Unit"/>

I have declared variable in Supplier.java as:

private Set <Unit> Units;

Solution

  • In your entity class Supplier put:

    private Set<Unit> units;
    
    @ManyToMany
    @JoinTable(name="UNIT", joinColumns={@JoinColumn(name="ID")}, 
               inverseJoinColumns={@JoinColumn(name="U_ID")}
    )
    public Set<Unit> getUnits() {
        return units;
    }