Hibernate many-to-many mapping not saved in pivot table

Posted by vincent on Stack Overflow See other posts from Stack Overflow or by vincent
Published on 2010-05-20T13:40:50Z Indexed on 2010/05/20 15:10 UTC
Read the original article Hit count: 313

Filed under:
|
|
|
|

I having problems saving many to many relationships to a pivot table.

The way the pojos are created is unfortunately a pretty long process which spans over a couple of different threads which work on the (to this point un-saved) object until it is finally persisted. I associate the related objects to one another right after they are created and when debugging I can see the List of related object populated with their respective objects. So basically all is fine to this point. When I persist the object everything get saved except the relations in the pivot table.

mapping files:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.thebeansgroup.jwinston.plugin.orm.hibernate.object">
    <class name="ShowObject" table="show_object">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name" />
        <set cascade="all" inverse="true" name="venues" table="venue_show">
            <key column="show_id"/>
            <many-to-many class="VenueObject"/>
        </set>
    </class>
</hibernate-mapping>

and the other

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.thebeansgroup.jwinston.plugin.orm.hibernate.object">
    <class name="VenueObject" table="venue_object">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <property name="latitude" type="integer"/>
        <property name="longitude" type="integer"/>
        <set cascade="all" inverse="true" name="shows" table="venue_show">
            <key column="venue_id"/>
            <many-to-many class="ShowObject"/>
        </set>
    </class>
</hibernate-mapping>

pojos:

public class ShowObject extends OrmObject
{

    private Long id;
    private String name;
    private Set venues;

    public ShowObject()
    {
    }

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Set getVenues()
    {
        return venues;
    }

    public void setVenues(Set venues)
    {
        this.venues = venues;
    }
}

and the other:

public class VenueObject extends OrmObject
{

    private Long id;
    private String name;
    private int latitude;
    private int longitude;
    private Set shows = new HashSet();

    public VenueObject()
    {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public int getLatitude()
    {
        return latitude;
    }

    public void setLatitude(int latitude)
    {
        this.latitude = latitude;
    }

    public int getLongitude()
    {
        return longitude;
    }

    public void setLongitude(int longitude)
    {
        this.longitude = longitude;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Set getShows()
    {
        return shows;
    }

    public void setShows(Set shows)
    {
        this.shows = shows;
    }
}

Might the problem be related to the lack of annotations?

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate