Hibernate version:2.1.6
Mapping documents: <class name="net.canal.admin.persistence.Program" table="PROGRAMS"> <id name="id" type="long" column="PROGRAM_ID" unsaved-value="0"> <generator class="identity"/> </id>
<!-- Program properties -->
<!-- Program News related properties --> <set name="news" lazy="true" table="PROGRAM_NEWS" order-by="PUBLISH_TIME asc"> <key column="PROGRAM_ID"/> <composite-element class="net.canal.admin.persistence.News"> <property name="headline" column="HEADLINE" /> <property name="author" column="AUTHOR" /> <property name="content" column="CONTENT" /> <property name="createTime" column="CREATE_TIME" /> <property name="publishTime" column="PUBLISH_TIME" /> </composite-element> </set>
<!-- Program Milestone properties --> <set name="milestones" lazy="true" table="PROGRAM_MILESTONE" order-by="MILESTONE_TIME asc"> <key column="PROGRAM_ID"/> <composite-element class="net.canal.admin.persistence.Milestone"> <property name="name" column="NAME" /> <property name="description" column="DESCR" /> <property name="milestoneTime" column="MILESTONE_TIME" /> </composite-element> </set>
</class>
Name and version of the database you are using:mySQL
The generated SQL table (show_sql=true): create table PROGRAMS ( PROGRAM_ID bigint not null auto_increment, NAME varchar(255) not null, DESCR varchar(255), ROOTFOLDER varchar(255), TOTALFILES bigint, TOTALACCESS bigint, ORG_ID bigint not null, primary key (PROGRAM_ID) ); create table PROGRAM_NEWS ( PROGRAM_ID bigint not null, HEADLINE varchar(255), AUTHOR varchar(255), CONTENT varchar(255), CREATE_TIME datetime, PUBLISH_TIME datetime );
Hi,
I am having a very strange result. I defined a Program entity which has one composite-element for News and one for Milestone.
I have no problem operating on Milestones, however, when I add one news, the existing news are copied and stored again. So if currently I have a program P1 which has 3 piece of news, after adding 1, I will get 3+1+3 = 7. I do not understand why or how to debug this.
The code that handles save/get/update news and milestones are very much the same. the definition of class News and Milestone are also very similar.
Here is how I add news to a program (I am using Spring framework):
......
News n = new News();
BeanUtils.copyProperties (n, ...);
Program p = manager.getProgram (pid):
p.addNews (n);
manager.saveProgram (p);
......
public class Program ... {
......
private Set news = new HashSet();
private Set milestones = new HashSet();
......
public Set getNews () {
return this.news;
}
public void setNews (Set news) {
this.news = news;
}
public void addNews (News news) {
this.news.add(news);
}
.....
}
The manager class is very simple (in Spring framework):
getHibernateTemplate().update(p);
The strange thing is that everything is fine for Milestone. I can not figure out the difference.
thanks for your reading !
li xin
|