Results 1 to 1 of 1
- 03-03-2010, 04:53 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 25
- Rep Power
- 0
Difficulties with many-to-many association
Hi!
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):
Here's the xml for the other side (item):Java Code:<class name="packPojo.Category" table="category"> <id name="id" column="category_id" type="long"> <generator class="native"/> </id> <property name="name" column="category_name" type="string"/> <set name="catItem" inverse="true"> <key column="category_id"/> <one-to-many class="packPojo.CategorizedItem"/> </set> </class>
Here's the xml for join table (CategorizedItem):Java Code:<class name="packPojo.Item" table="item"> <id name="id" column="item_id" type="long"> <generator class="native"/> </id> <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"/> </set> </class>
...and the pojo class for 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"/> </composite-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"/> </class>
The other pojo classes have classic one-to-many collections with the join table.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) { this.categoryId=categoryId; this.itemId=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);} else {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 this.id.categoryId=category.getId(); this.id.itemId=item.getId(); //Guarantee referential integrity category.getCatItem().add(this); item.getCategorizedItem().add(this); } 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; } }
So, here's the code of the main class:
...and the error I get: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(aCategory); session.save(anItem1); session.save(anItem2); session.save(anItem3); session.save(link1); // session.save(link2); // session.save(link3);
Has anyone a suggestion...I've tried everything so far...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
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!
Similar Threads
-
Difficulties using Hibernate + MySQL
By berlindutza in forum Web FrameworksReplies: 3Last Post: 02-17-2010, 09:23 AM -
RFC 2109 (Association of cookie) - why does it have to follow the regulation?
By javanewbie in forum New To JavaReplies: 1Last Post: 11-07-2008, 05:49 PM -
UML association,aggregation etc generator
By alexander.s in forum New To JavaReplies: 1Last Post: 09-18-2008, 06:27 PM -
JOptionPane Display Difficulties
By kewlgeye in forum New To JavaReplies: 7Last Post: 05-09-2008, 08:09 PM -
Mapping of class Association with hibernate
By Albert in forum JDBCReplies: 1Last Post: 07-06-2007, 06:27 AM


LinkBack URL
About LinkBacks

Bookmarks