Hi there,
I would like to swap two elements in a list, but I'm not able to do it. Here is my code:
Code:
Session hibernateSession = sessionFactory.openSession( );
Transaction transaction = hibernateSession.beginTransaction( );
try
{
String categoryId = httpServletRequest.getParameter( "categoryId" );
Category category = (Category) hibernateSession.load( Category.class, categoryId );
Publication publication = category.getPublication( );
int categoryIndex = publication.getCategories( ).indexOf( category );
if( categoryIndex > 0 )
{
Category swappedCategory = (Category) publication.getCategories( ).remove( categoryIndex - 1 );
publication.getCategories( ).add( categoryIndex, swappedCategory );
}
hibernateSession.update( publication );
transaction.commit( );
}
finally
{
hibernateSession.close( );
}
Here is my mapping:
Code:
<list name="categories" lazy="false" inverse="true" cascade="all">
<key column="publication"/>
<index column="rank" type="integer" length="10"/>
<one-to-many class="com.actaris.news.domain.Category"/>
</list>
............
<class name="com.actaris.news.domain.Category" table="category" dynamic-update="false" dynamic-insert="false">
<many-to-one name="publication" class="com.actaris.news.domain.Publication" cascade="none" outer-join="auto" update="true" insert="true" column="publication" not-null="true" unique="false"/>
Am I doing anything stupid or what?
Regards
Jose