Hi all,
I am new to hibernate and spring and I've tried to model my code after the petclinic example and the hibernate book.
I have a class Prof that has one-to-many relationship with class Pattern. I have another class Type that also has one-to-many relationship with class Pattern. Pattern has 2 many-to-one relationships with Type and Prof. I am getting a stack overflow exception when I try to save a pattern. Inside the Prof and Type POJOs, I declared Set patterns. Before I save the Pattern, I try to add the pattern to the set in Prof and the set in Type. The exception occurs when I try to add the pattern to the second set. It doesn't matter which one I add first, I switched the order and it always occurs on the second time I try to add. Does anyone have any idea what I am doing wrong? Any help is appreciated!
Here is the table structure:
Table Prof
pro_id PK
Table Type
type_id PK
Table Pattern
pat_id PK
pro_id FK
type_id FK
Below is the mapping file, pojo's and code. I followed the petclinic example and the hibernate book to get this code.
Hibernate Mapping
snippet of test case:Code:<hibernate-mapping> <class name="Prof" table="prof" > <id name="pro_id" column="pro_id" type="java.lang.Integer"> <generator class="increment"> </generator> </id> <set name="patterns" inverse="true" > <key column="pro_id"></key> <one-to-many class="Pattern"/> </set> </class> <class name="Pattern" table="pattern" > <id name="pat_id" column="pat_id" type="java.lang.Integer"> <generator class="increment"></generator> </id> <many-to-one name="prof" class="Prof" column="pro_id" /> <many-to-one name="type" class="Type" column="type_id" /> </class> <class name="Type" table="type" > <id name="type_id" column="type_id" type="java.lang.Integer"> <generator class="increment"></generator> </id> <set name="patterns" inverse="true" > <key column="type_id"></key> <one-to-many class="Pattern"/> </set> </class> </hibernate-mapping>
POJO'sCode:...get prof from db... ...get typ from db... pattern = new Pattern() ...set pattern properties... typ.addPattern(pattern); prof.addPattern(pattern); <--second time add pattern to a set, stack overflow(see Prof POJO) dao.savePattern(pattern);
thanks in advance.Code:public class Prof extends BaseObject{ private Integer pro_id; private Set patterns; ... public Set getPatterns() { if (this.patterns == null){ this.patterns = new HashSet(); } return this.patterns; } public void setPatterns(Set patterns) { this.patterns = patterns; } public void addPattern(Pattern ptn){ getPatterns().add(ptn); <--this line causes stack overflow ptn.setProf(this); } } public class Pattern extends BaseObject { private Integer pat_id; private Prof prof; private Type typ; ... public Prof getProf() { return prof; } public void setProf(Prof prof) { this.prof = prof; } public Type getType() { return typ; } public void setType(Type typ) { this.typ = typ; } } public class Type extends BaseObject{ private Integer type_id; private Set patterns; ... public Set getPatterns() { if (this.patterns == null){ this.patterns = new HashSet(); } return this.patterns; } public void setPatterns(Set patterns) { this.patterns = patterns; } public void addPattern(Pattern ptn){ getPatterns().add(ptn); ptn.setType(this); } }
pat


Reply With Quote