开发者

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

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?


Couple things to try:

  1. You have inverse="true" on both ends of many-to-many. It should be only at one end.

  2. Make your sets not lazy.

  3. You didn't specify column property for many-to-many tag.

So it should look something like this at the end:

<class name="ShowObject" table="show_object">
    ...
    <set lazy="false" cascade="all" name="venues" table="venue_show">
        <key column="show_id"/>
        <many-to-many class="VenueObject" column="venue_id" />
    </set>
</class>

<class name="VenueObject" table="venue_object">
    ...
    <set lazy="false" cascade="all" inverse="true" name="shows" table="venue_show">
        <key column="venue_id"/>
        <many-to-many class="ShowObject" column="show_id"/>
    </set>
</class>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜