I'm trying to implement a many-to-many association (between category and item) by mapping the join table to an intermediate entity.

Here's the xml for one side (category):

Java Code:
<class name="packPojo.Category" table="category">
        <id name="id" column="category_id" type="long">
               <generator class="native"/>
        <property name="name" column="category_name" type="string"/>
        <set name="catItem" inverse="true">
           <key column="category_id"/>
           <one-to-many class="packPojo.CategorizedItem"/>
Here's the xml for the other side (item):

Java Code:
<class name="packPojo.Item" table="item">
       <id name="id" column="item_id" type="long">
           <generator class="native"/>
        <property name="name" column="item_name" type="string" />
        <property name="description" column="description" type="string"/>
        <set name="categorizedItem" inverse="true">
           <key column="item_id"/>
           <one-to-many  class="packPojo.CategorizedItem"/>
Here's the xml for join table (CategorizedItem):

Java Code:
<class name="packPojo.CategorizedItem" table="category_item" mutable="false">
      <composite-id name="id" class="packPojo.CategorizedItem$Id">
         <key-property name="categoryId" access="field"  column="category_id"/>
         <key-property  name="itemId" access="field"  column="item_id"/>
      <property name="dateAdded" column="added_on" type="timestamp" />
      <property name="username" column="added_by_user" type="string" />
      <many-to-one name="item" column="item_id" update="false" insert="false" not-null="true" class="packPojo.Item"/>
      <many-to-one name="category" column="category_id" update="false" insert="false" not-null="true" class="packPojo.Category"/>
...and the pojo class for CategorizedItem:

Java Code:
public class CategorizedItem {

	public static class Id implements Serializable
	   private Long categoryId;
	   private Long itemId;
	   public Id(){}
	   public Id(Long categoryId, Long itemId)
	   public int hashCode()
	    return categoryId.hashCode()+itemId.hashCode();
	   public boolean equals(Object o)
		   if (o != null && o instanceof Id)
			   Id that=(Id)o;  
			   return this.categoryId.equals(that.categoryId)&& this.itemId.equals(that.itemId);}
		       {return false;}
	private Id id=new Id();
	private String username;
	private Date dateAdded=new Date();
	private Item item;
	private Category category;
	public CategorizedItem(){}
	public CategorizedItem(String username, Category category, Item item) {
		this.username = username;
		this.item = item;
		this.category = category;
		//Set identifier values
	   //Guarantee referential integrity

	public Id getId() {
		return id;

	public void setId(Id id) {
		this.id = id;

	public String getUsername() {
		return username;

	public void setUsername(String username) {
		this.username = username;

	public Date getDateAdded() {
		return dateAdded;

	public void setDateAdded(Date dateAdded) {
		this.dateAdded = dateAdded;

	public Item getItem() {
		return item;

	public void setItem(Item item) {
		this.item = item;

	public Category getCategory() {
		return category;

	public void setCategory(Category category) {
		this.category = category;
The other pojo classes have classic one-to-many collections with the join table.
So, here's the code of the main class:

Java Code:
 Category aCategory=new Category("toys");
			 Item anItem1=new Item("Papusa","Blonda");
			 Item anItem2=new Item("Masina","Blindata");
			 Item anItem3=new Item("Cal","Cu coama");
			 CategorizedItem link1=new CategorizedItem("user1",aCategory,anItem1);
			 CategorizedItem link2=new CategorizedItem("user2",aCategory,anItem2);
			 CategorizedItem link3=new CategorizedItem("user3",aCategory,anItem3);
		//	 session.save(link2);
			// session.save(link3);
...and the error I get:

Java Code:
Hibernate: insert into category (category_name) values (?)
Hibernate: insert into item (item_name, description) values (?, ?)
Hibernate: insert into item (item_name, description) values (?, ?)
Hibernate: insert into item (item_name, description) values (?, ?)
Hibernate: insert into category_item (added_on, added_by_user, category_id, item_id) values (?, ?, ?, ?)
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
	at packPojo.Main.main(Main.java:58)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails (`multi`.`category_item`, CONSTRAINT `FK142DE5B462781AAB` FOREIGN KEY (`category_id`) REFERENCES `category` (`category_id`))"
	at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1540)
	at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
	at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
	... 8 more
Has anyone a suggestion...I've tried everything so far...

And another question: when I insert data into a database from the main class (like I'm trying in the upper example), then the previous data is deleted from the table when I run the main class again. Does anyone now why? If I'm using DAO classes, then the old rows don't get deleted every time I run the main class.

Thanks a lot!